1 /* Operating system support for run-time dynamic linker. Hurd version.
2 Copyright (C) 1995-2023 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 <https://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. */
34 #include <mach/mig_support.h>
35 #include <mach/machine/vm_param.h>
36 #include "hurdstartup.h"
37 #include <hurd/lookup.h>
38 #include <hurd/auth.h>
39 #include <hurd/term.h>
46 #include <dl-machine.h>
47 #include <dl-procinfo.h>
49 #include <dl-tunables.h>
50 #include <not-errno.h>
51 #include <not-cancel.h>
53 extern void __mach_init (void);
56 extern char **_dl_argv
;
57 extern char **_environ
;
59 int __libc_enable_secure
= 0;
60 rtld_hidden_data_def (__libc_enable_secure
)
61 /* This variable contains the lowest stack address ever used. */
62 void *__libc_stack_end
= NULL
;
63 rtld_hidden_data_def(__libc_stack_end
)
65 /* TODO: Initialize. */
66 void *_dl_random attribute_relro
= NULL
;
68 struct hurd_startup_data
*_dl_hurd_data
;
72 _dl_sysdep_start (void **start_argptr
,
73 void (*dl_main
) (const ElfW(Phdr
) *phdr
, ElfW(Word
) phent
,
74 ElfW(Addr
) *user_entry
,
77 void go (intptr_t *argdata
)
82 /* Cache the information in various global variables. */
84 _dl_argv
= 1 + (char **) argdata
;
85 _environ
= &_dl_argv
[_dl_argc
+ 1];
86 for (p
= _environ
; *p
++;); /* Skip environ pointers and terminator. */
88 orig_argv0
= _dl_argv
[0];
90 if ((void *) p
== _dl_argv
[0])
92 static struct hurd_startup_data nodata
;
93 _dl_hurd_data
= &nodata
;
94 nodata
.user_entry
= (vm_address_t
) ENTRY_POINT
;
97 _dl_hurd_data
= (void *) p
;
99 GLRO(dl_platform
) = NULL
; /* Default to nothing known about the platform. */
101 __libc_enable_secure
= _dl_hurd_data
->flags
& EXEC_SECURE
;
103 __tunables_init (_environ
);
105 /* Initialize DSO sorting algorithm after tunables. */
106 _dl_sort_maps_init ();
108 #ifdef DL_SYSDEP_INIT
112 #ifdef DL_PLATFORM_INIT
116 /* Determine the length of the platform name. */
117 if (GLRO(dl_platform
) != NULL
)
118 GLRO(dl_platformlen
) = strlen (GLRO(dl_platform
));
120 if (_dl_hurd_data
->flags
& EXEC_STACK_ARGS
121 && _dl_hurd_data
->user_entry
== 0)
122 _dl_hurd_data
->user_entry
= (vm_address_t
) ENTRY_POINT
;
124 #if 0 /* XXX make this work for real someday... */
125 if (_dl_hurd_data
->user_entry
== (vm_address_t
) ENTRY_POINT
)
126 /* We were invoked as a command, not as the program interpreter.
127 The generic ld.so code supports this: it will parse the args
128 as "ld.so PROGRAM [ARGS...]". For booting the Hurd, we
129 support an additional special syntax:
130 ld.so [-LIBS...] PROGRAM [ARGS...]
131 Each LIBS word consists of "FILENAME=MEMOBJ";
132 for example "-/lib/libc.so=123" says that the contents of
133 /lib/libc.so are found in a memory object whose port name
134 in our task is 123. */
135 while (_dl_argc
> 2 && _dl_argv
[1][0] == '-' && _dl_argv
[1][1] != '-')
137 char *lastslash
, *memobjname
, *p
;
143 p
= _dl_argv
++[1] + 1;
145 memobjname
= strchr (p
, '=');
147 _dl_sysdep_fatal ("Bogus library spec: ", p
, "\n", NULL
);
148 *memobjname
++ = '\0';
150 while (*memobjname
!= '\0')
151 memobj
= (memobj
* 10) + (*memobjname
++ - '0');
153 /* Add a user reference on the memory object port, so we will
154 still have one after _dl_map_object_from_fd calls our
156 err
= __mach_port_mod_refs (__mach_task_self (), memobj
,
157 MACH_PORT_RIGHT_SEND
, +1);
160 lastslash
= strrchr (p
, '/');
161 l
= _dl_map_object_from_fd (lastslash
? lastslash
+ 1 : p
, NULL
,
162 memobj
, strdup (p
), 0);
164 /* Squirrel away the memory object port where it
165 can be retrieved by the program later. */
166 l
->l_info
[DT_NULL
] = (void *) memobj
;
170 /* Call elf/rtld.c's main program. It will set everything
171 up and leave us to transfer control to USER_ENTRY. */
172 (*dl_main
) ((const ElfW(Phdr
) *) _dl_hurd_data
->phdr
,
173 _dl_hurd_data
->phdrsz
/ sizeof (ElfW(Phdr
)),
174 (ElfW(Addr
) *) &_dl_hurd_data
->user_entry
, NULL
);
176 /* The call above might screw a few things up.
178 P is the location after the terminating NULL of the list of
179 environment variables. It has to point to the Hurd startup
180 data or if that's missing then P == ARGV[0] must hold. The
181 startup code in init-first.c will get confused if this is not
182 the case, so we must rearrange things to make it so. We'll
183 recompute P and move the Hurd data or the new ARGV[0] there.
185 Note: directly invoked ld.so can move arguments and env vars.
187 We use memmove, since the locations might overlap. */
190 for (newp
= _environ
; *newp
++;);
192 if (newp
!= p
|| _dl_argv
[0] != orig_argv0
)
194 if (orig_argv0
== (char *) p
)
196 if ((char *) newp
!= _dl_argv
[0])
198 assert ((char *) newp
< _dl_argv
[0]);
199 _dl_argv
[0] = memmove ((char *) newp
, _dl_argv
[0],
200 strlen (_dl_argv
[0]) + 1);
205 if ((void *) newp
!= _dl_hurd_data
)
206 memmove (newp
, _dl_hurd_data
, sizeof (*_dl_hurd_data
));
211 extern void _dl_start_user (void);
212 /* Unwind the stack to ARGDATA and simulate a return from _dl_start
213 to the RTLD_START code which will run the user's entry point. */
214 RETURN_TO (argdata
, &_dl_start_user
, _dl_hurd_data
->user_entry
);
218 /* Set up so we can do RPCs. */
221 /* Initialize frequently used global variable. */
222 GLRO(dl_pagesize
) = __getpagesize ();
224 /* See hurd/hurdstartup.c; this deals with getting information
225 from the exec server and slicing up the arguments.
226 Then it will call `go', above. */
227 _hurd_startup (start_argptr
, &go
);
234 _dl_sysdep_start_cleanup (void)
236 /* Deallocate the reply port and task port rights acquired by
237 __mach_init. We are done with them now, and the user will
238 reacquire them for himself when he wants them. */
239 __mig_dealloc_reply_port (MACH_PORT_NULL
);
240 __mach_port_deallocate (__mach_task_self (), __mach_host_self_
);
241 __mach_port_deallocate (__mach_task_self (), __mach_task_self_
);
244 /* Minimal open/close/mmap/etc. implementation sufficient for initial loading of
245 shared libraries. These are weak definitions so that when the
246 dynamic linker re-relocates itself to be user-visible (for -ldl),
247 it will get the user's definition (i.e. usually libc's).
249 They also need to be set in the libc and ld section of
250 sysdeps/mach/hurd/Versions, to be overridable, and in libc.abilist and
251 ld.abilist to be checked. */
253 /* This macro checks that the function does not get renamed to be hidden: we do
254 need these to be overridable by libc's. */
255 #define check_no_hidden(name) \
256 __typeof (name) __check_##name##_no_hidden \
257 __attribute__ ((alias (#name))) \
258 __attribute_copy__ (name);
260 /* Open FILE_NAME and return a Hurd I/O for it in *PORT, or return an
261 error. If STAT is non-zero, stat the file into that stat buffer. */
263 open_file (const char *file_name
, int flags
,
264 mach_port_t
*port
, struct stat64
*stat
)
266 enum retry_type doretry
;
267 char retryname
[1024]; /* XXX string_t LOSES! */
271 error_t
use_init_port (int which
, error_t (*operate
) (file_t
))
273 return (which
< _dl_hurd_data
->portarraysize
274 ? ((*operate
) (_dl_hurd_data
->portarray
[which
]))
277 file_t
get_dtable_port (int fd
)
279 if ((unsigned int) fd
< _dl_hurd_data
->dtablesize
280 && _dl_hurd_data
->dtable
[fd
] != MACH_PORT_NULL
)
282 __mach_port_mod_refs (__mach_task_self (), _dl_hurd_data
->dtable
[fd
],
283 MACH_PORT_RIGHT_SEND
, +1);
284 return _dl_hurd_data
->dtable
[fd
];
287 return MACH_PORT_NULL
;
290 assert (!(flags
& ~(O_READ
| O_EXEC
| O_CLOEXEC
)));
293 startdir
= _dl_hurd_data
->portarray
[file_name
[0] == '/'
294 ? INIT_PORT_CRDIR
: INIT_PORT_CWDIR
];
296 while (file_name
[0] == '/')
299 err
= __dir_lookup (startdir
, (char *)file_name
, flags
, 0,
300 &doretry
, retryname
, port
);
303 err
= __hurd_file_name_lookup_retry (use_init_port
, get_dtable_port
,
304 __dir_lookup
, doretry
, retryname
,
308 err
= __io_stat (*port
, stat
);
310 __mach_port_deallocate (__mach_task_self (), *port
);
316 check_no_hidden(__open
);
317 check_no_hidden (__open64
);
318 check_no_hidden (__open_nocancel
);
320 __open (const char *file_name
, int mode
, ...)
323 error_t err
= open_file (file_name
, mode
, &port
, 0);
325 return __hurd_fail (err
);
329 weak_alias (__open
, __open64
)
330 weak_alias (__open
, __open_nocancel
)
332 check_no_hidden(__close
);
333 check_no_hidden(__close_nocancel
);
337 if (fd
!= (int) MACH_PORT_NULL
)
338 __mach_port_deallocate (__mach_task_self (), (mach_port_t
) fd
);
341 weak_alias (__close
, __close_nocancel
)
343 check_no_hidden(__pread64
);
344 check_no_hidden(__pread64_nocancel
);
345 __ssize_t weak_function
346 __pread64 (int fd
, void *buf
, size_t nbytes
, off64_t offset
)
350 mach_msg_type_number_t nread
;
354 err
= __io_read ((mach_port_t
) fd
, &data
, &nread
, offset
, nbytes
);
356 return __hurd_fail (err
);
360 memcpy (buf
, data
, nread
);
361 __vm_deallocate (__mach_task_self (), (vm_address_t
) data
, nread
);
366 libc_hidden_weak (__pread64
)
367 weak_alias (__pread64
, __pread64_nocancel
)
369 check_no_hidden(__read
);
370 check_no_hidden(__read_nocancel
);
371 __ssize_t weak_function
372 __read (int fd
, void *buf
, size_t nbytes
)
374 return __pread64 (fd
, buf
, nbytes
, -1);
376 libc_hidden_weak (__read
)
377 weak_alias (__read
, __read_nocancel
)
379 check_no_hidden(__write
);
380 check_no_hidden(__write_nocancel
);
381 __ssize_t weak_function
382 __write (int fd
, const void *buf
, size_t nbytes
)
387 assert (fd
< _hurd_init_dtablesize
);
389 err
= __io_write (_hurd_init_dtable
[fd
], buf
, nbytes
, -1, &nwrote
);
391 return __hurd_fail (err
);
395 libc_hidden_weak (__write
)
396 weak_alias (__write
, __write_nocancel
)
398 /* This is only used for printing messages (see dl-misc.c). */
399 check_no_hidden(__writev
);
400 __ssize_t weak_function
401 __writev (int fd
, const struct iovec
*iov
, int niov
)
403 if (fd
>= _hurd_init_dtablesize
)
411 for (i
= 0; i
< niov
; ++i
)
412 total
+= iov
[i
].iov_len
;
416 char buf
[total
], *bufp
= buf
;
420 for (i
= 0; i
< niov
; ++i
)
421 bufp
= (memcpy (bufp
, iov
[i
].iov_base
, iov
[i
].iov_len
)
424 err
= __io_write (_hurd_init_dtable
[fd
], buf
, total
, -1, &nwrote
);
426 return __hurd_fail (err
);
433 check_no_hidden(__libc_lseek64
);
434 off64_t weak_function
435 __libc_lseek64 (int fd
, off64_t offset
, int whence
)
439 err
= __io_seek ((mach_port_t
) fd
, offset
, whence
, &offset
);
441 return __hurd_fail (err
);
446 check_no_hidden(__mmap
);
448 __mmap (void *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 if (memobj_wr
!= MACH_PORT_NULL
)
472 __mach_port_deallocate (__mach_task_self (), memobj_wr
);
475 mapaddr
= (vm_address_t
) addr
;
476 err
= __vm_map (__mach_task_self (),
477 &mapaddr
, (vm_size_t
) len
, 0,
478 !(flags
& MAP_FIXED
),
480 (vm_offset_t
) offset
,
481 flags
& (MAP_COPY
|MAP_PRIVATE
),
483 (flags
& MAP_SHARED
) ? VM_INHERIT_SHARE
: VM_INHERIT_COPY
);
484 if (err
== KERN_NO_SPACE
&& (flags
& MAP_FIXED
))
486 /* XXX this is not atomic as it is in unix! */
487 /* The region is already allocated; deallocate it first. */
488 err
= __vm_deallocate (__mach_task_self (), mapaddr
, len
);
490 err
= __vm_map (__mach_task_self (),
491 &mapaddr
, (vm_size_t
) len
,
493 !(flags
& MAP_FIXED
),
494 memobj_rd
, (vm_offset_t
) offset
,
495 flags
& (MAP_COPY
|MAP_PRIVATE
),
498 ? VM_INHERIT_SHARE
: VM_INHERIT_COPY
);
501 if ((flags
& MAP_ANON
) == 0)
502 __mach_port_deallocate (__mach_task_self (), memobj_rd
);
505 return __hurd_fail (err
), MAP_FAILED
;
506 return (void *) mapaddr
;
509 check_no_hidden(__fstat64
);
511 __fstat64 (int fd
, struct stat64
*buf
)
515 err
= __io_stat ((mach_port_t
) fd
, buf
);
517 return __hurd_fail (err
);
521 libc_hidden_def (__fstat64
)
523 check_no_hidden(__stat64
);
525 __stat64 (const char *file
, struct stat64
*buf
)
530 err
= open_file (file
, 0, &port
, buf
);
532 return __hurd_fail (err
);
534 __mach_port_deallocate (__mach_task_self (), port
);
538 libc_hidden_def (__stat64
)
540 /* This function is called by the dynamic linker (rtld.c) to check
541 whether debugging malloc is allowed even for SUID binaries. This
542 stub will always fail, which means that malloc-debugging is always
543 disabled for SUID binaries. */
544 check_no_hidden(__access
);
546 __access (const char *file
, int type
)
551 check_no_hidden(__access_noerrno
);
553 __access_noerrno (const char *file
, int type
)
559 __rtld_execve (const char *file_name
, char *const argv
[],
565 size_t argslen
, envlen
;
566 mach_port_t
*ports
= _dl_hurd_data
->portarray
;
567 unsigned int portarraysize
= _dl_hurd_data
->portarraysize
;
568 file_t
*dtable
= _dl_hurd_data
->dtable
;
569 unsigned int dtablesize
= _dl_hurd_data
->dtablesize
;
570 int *intarray
= _dl_hurd_data
->intarray
;
572 mach_port_t
*please_dealloc
, *pdp
;
573 mach_port_t
*portnames
= NULL
;
574 mach_msg_type_number_t nportnames
= 0;
575 mach_port_type_t
*porttypes
= NULL
;
576 mach_msg_type_number_t nporttypes
= 0;
579 err
= open_file (file_name
, O_EXEC
, &file
, NULL
);
584 args
= NULL
, argslen
= 0;
585 else if (err
= __argz_create (argv
, &args
, &argslen
))
588 env
= NULL
, envlen
= 0;
589 else if (err
= __argz_create (envp
, &env
, &envlen
))
592 please_dealloc
= __alloca ((portarraysize
+ dtablesize
)
593 * sizeof (mach_port_t
));
594 pdp
= please_dealloc
;
596 /* Get all ports that we may not know about and we should thus destroy. */
597 err
= __mach_port_names (__mach_task_self (),
598 &portnames
, &nportnames
,
599 &porttypes
, &nporttypes
);
602 if (nportnames
!= nporttypes
)
608 for (i
= 0; i
< portarraysize
; ++i
)
609 if (ports
[i
] != MACH_PORT_NULL
)
612 for (j
= 0; j
< nportnames
; j
++)
613 if (portnames
[j
] == ports
[i
])
614 portnames
[j
] = MACH_PORT_NULL
;
616 for (i
= 0; i
< dtablesize
; ++i
)
617 if (dtable
[i
] != MACH_PORT_NULL
)
620 for (j
= 0; j
< nportnames
; j
++)
621 if (portnames
[j
] == dtable
[i
])
622 portnames
[j
] = MACH_PORT_NULL
;
625 /* Pack ports to be destroyed together. */
626 for (i
= 0, j
= 0; i
< nportnames
; i
++)
628 if (portnames
[i
] == MACH_PORT_NULL
)
631 portnames
[j
] = portnames
[i
];
638 if (__sigismember (&intarray
[INIT_TRACEMASK
], SIGKILL
))
639 flags
|= EXEC_SIGTRAP
;
642 err
= __file_exec_paths (file
, __mach_task_self (), flags
,
643 file_name
, file_name
[0] == '/' ? file_name
: "",
646 dtable
, MACH_MSG_TYPE_COPY_SEND
, dtablesize
,
647 ports
, MACH_MSG_TYPE_COPY_SEND
, portarraysize
,
648 intarray
, INIT_INT_MAX
,
649 please_dealloc
, pdp
- please_dealloc
,
650 portnames
, nportnames
);
652 /* Oh well. Might as well be tidy. */
658 __mach_port_deallocate (__mach_task_self (), file
);
663 check_no_hidden(__getpid
);
670 if (__proc_getpids (_dl_hurd_data
->portarray
[INIT_PORT_PROC
],
671 &pid
, &ppid
, &orphaned
))
677 /* We need this alias to satisfy references from libc_pic.a objects
678 that were affected by the libc_hidden_proto declaration for __getpid. */
679 strong_alias (__getpid
, __GI___getpid
)
681 /* This is called only in some strange cases trying to guess a value
682 for $ORIGIN for the executable. The dynamic linker copes with
683 getcwd failing (dl-object.c), and it's too much hassle to include
684 the functionality here. (We could, it just requires duplicating or
685 reusing getcwd.c's code but using our special lookup function as in
687 check_no_hidden(__getcwd
);
689 __getcwd (char *buf
, size_t size
)
695 /* This is used by dl-tunables.c to strdup strings. We can just make this a
697 check_no_hidden(__sbrk
);
699 __sbrk (intptr_t increment
)
702 if (__vm_allocate (__mach_task_self (), &addr
, increment
, 1))
704 return (void *) addr
;
707 /* This is only used by hurdlookup for the /dev/fd/nnn magic.
708 * We avoid pulling the whole libc implementation, and we can keep this hidden. */
709 unsigned long int weak_function
710 __strtoul_internal (const char *nptr
, char **endptr
, int base
, int group
)
712 assert (base
== 0 || base
== 10);
714 return _dl_strtoul (nptr
, endptr
);
717 /* We need this alias to satisfy references from libc_pic.a objects
718 that were affected by the libc_hidden_proto declaration for __strtoul_internal. */
719 strong_alias (__strtoul_internal
, __GI___strtoul_internal
)
720 strong_alias (__strtoul_internal
, __GI_____strtoul_internal
)
722 check_no_hidden(_exit
);
723 void weak_function attribute_hidden
726 __proc_mark_exit (_dl_hurd_data
->portarray
[INIT_PORT_PROC
],
727 W_EXITCODE (status
, 0), 0);
728 while (__task_terminate (__mach_task_self ()))
729 __mach_task_self_
= (__mach_task_self
) ();
734 /* We need this alias to satisfy references from libc_pic.a objects
735 that were affected by the libc_hidden_proto declaration for _exit. */
736 strong_alias (_exit
, __GI__exit
)
738 /* Try to get a machine dependent instruction which will make the
739 program crash. This is used in case everything else fails. */
740 #include <abort-instr.h>
741 #ifndef ABORT_INSTRUCTION
742 /* No such instruction is available. */
743 # define ABORT_INSTRUCTION
746 check_no_hidden(abort
);
750 /* Try to abort using the system specific command. */
753 /* If the abort instruction failed, exit. */
756 /* If even this fails, make sure we never return. */
758 /* Try for ever and ever. */
762 /* We need this alias to satisfy references from libc_pic.a objects
763 that were affected by the libc_hidden_proto declaration for abort. */
764 strong_alias (abort
, __GI_abort
)
765 strong_alias (abort
, __GI___fortify_fail
)
766 strong_alias (abort
, __GI___assert_fail
)
767 strong_alias (abort
, __GI___assert_perror_fail
)
769 /* This function is called by interruptible RPC stubs. For initial
770 dynamic linking, just use the normal mach_msg. Since this defn is
771 weak, the real defn in libc.so will override it if we are linked into
772 the user program (-ldl). */
774 error_t weak_function
775 _hurd_intr_rpc_mach_msg (mach_msg_header_t
*msg
,
776 mach_msg_option_t option
,
777 mach_msg_size_t send_size
,
778 mach_msg_size_t rcv_size
,
779 mach_port_t rcv_name
,
780 mach_msg_timeout_t timeout
,
783 return __mach_msg (msg
, option
, send_size
, rcv_size
, rcv_name
,
791 /* There is nothing to print. Hurd has no auxiliary vector. */
796 _dl_init_first (int argc
, ...)
798 /* This no-op definition only gets used if libc is not linked in. */