Use libc_hidden_* for fputs (bug 15105).
[glibc.git] / sysdeps / mach / hurd / dl-sysdep.c
blobef96df054e754ecc62da63c617fd60980a7e2fef
1 /* Operating system support for run-time dynamic linker. Hurd version.
2 Copyright (C) 1995-2018 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. */
21 #ifdef SHARED
23 #include <hurd.h>
24 #include <link.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <stdlib.h>
28 #include <sys/mman.h>
29 #include <ldsodefs.h>
30 #include <sys/wait.h>
31 #include <assert.h>
32 #include <sysdep.h>
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>
38 #include <stdarg.h>
39 #include <ctype.h>
40 #include <sys/stat.h>
41 #include <sys/uio.h>
43 #include <entry.h>
44 #include <dl-machine.h>
45 #include <dl-procinfo.h>
47 #include <dl-tunables.h>
49 extern void __mach_init (void);
51 extern int _dl_argc;
52 extern char **_dl_argv;
53 extern char **_environ;
55 int __libc_enable_secure = 0;
56 rtld_hidden_data_def (__libc_enable_secure)
57 int __libc_multiple_libcs = 0; /* Defining this here avoids the inclusion
58 of init-first. */
59 /* This variable contains the lowest stack address ever used. */
60 void *__libc_stack_end = NULL;
61 rtld_hidden_data_def(__libc_stack_end)
63 #if HP_TIMING_AVAIL
64 hp_timing_t _dl_cpuclock_offset;
65 #endif
68 struct hurd_startup_data *_dl_hurd_data;
70 /* This is used only within ld.so, via dl-minimal.c's __errno_location. */
71 #undef errno
72 int errno attribute_hidden;
74 /* Defining these variables here avoids the inclusion of hurdsig.c. */
75 unsigned long int __hurd_sigthread_stack_base;
76 unsigned long int __hurd_sigthread_stack_end;
77 unsigned long int *__hurd_sigthread_variables;
79 /* Defining these variables here avoids the inclusion of init-first.c.
80 We need to provide temporary storage for the per-thread variables
81 of the main user thread here, since it is used for storing the
82 `errno' variable. Note that this information is lost once we
83 relocate the dynamic linker. */
84 static unsigned long int threadvars[_HURD_THREADVAR_MAX];
85 unsigned long int __hurd_threadvar_stack_offset
86 = (unsigned long int) &threadvars;
87 unsigned long int __hurd_threadvar_stack_mask;
89 #define FMH defined(__i386__)
90 #if ! FMH
91 # define fmh() ((void)0)
92 # define unfmh() ((void)0)
93 #else
94 /* XXX loser kludge for vm_map kernel bug */
95 #undef ELF_MACHINE_USER_ADDRESS_MASK
96 #define ELF_MACHINE_USER_ADDRESS_MASK 0
97 static vm_address_t fmha;
98 static vm_size_t fmhs;
99 static void unfmh(void){
100 __vm_deallocate(__mach_task_self(),fmha,fmhs);}
101 static void fmh(void) {
102 error_t err;int x;vm_offset_t o;mach_port_t p;
103 vm_address_t a=0x08000000U,max=VM_MAX_ADDRESS;
104 while (!(err=__vm_region(__mach_task_self(),&a,&fmhs,&x,&x,&x,&x,&p,&o))){
105 __mach_port_deallocate(__mach_task_self(),p);
106 if (a+fmhs>=0x80000000U){
107 max=a; break;}
108 fmha=a+=fmhs;}
109 if (err) assert(err==KERN_NO_SPACE);
110 if (!fmha)fmhs=0;else{
111 fmhs=max-fmha;
112 err = __vm_map (__mach_task_self (),
113 &fmha, fmhs, 0, 0, MACH_PORT_NULL, 0, 1,
114 VM_PROT_NONE, VM_PROT_NONE, VM_INHERIT_COPY);
115 assert_perror(err);}
117 /* XXX loser kludge for vm_map kernel bug */
118 #endif
121 ElfW(Addr)
122 _dl_sysdep_start (void **start_argptr,
123 void (*dl_main) (const ElfW(Phdr) *phdr, ElfW(Word) phent,
124 ElfW(Addr) *user_entry,
125 ElfW(auxv_t) *auxv))
127 void go (intptr_t *argdata)
129 char **p;
131 /* Cache the information in various global variables. */
132 _dl_argc = *argdata;
133 _dl_argv = 1 + (char **) argdata;
134 _environ = &_dl_argv[_dl_argc + 1];
135 for (p = _environ; *p++;); /* Skip environ pointers and terminator. */
137 if ((void *) p == _dl_argv[0])
139 static struct hurd_startup_data nodata;
140 _dl_hurd_data = &nodata;
141 nodata.user_entry = (vm_address_t) ENTRY_POINT;
143 else
144 _dl_hurd_data = (void *) p;
146 __libc_enable_secure = _dl_hurd_data->flags & EXEC_SECURE;
148 __tunables_init (_environ);
150 if (_dl_hurd_data->flags & EXEC_STACK_ARGS &&
151 _dl_hurd_data->user_entry == 0)
152 _dl_hurd_data->user_entry = (vm_address_t) ENTRY_POINT;
154 unfmh(); /* XXX */
156 #if 0 /* XXX make this work for real someday... */
157 if (_dl_hurd_data->user_entry == (vm_address_t) ENTRY_POINT)
158 /* We were invoked as a command, not as the program interpreter.
159 The generic ld.so code supports this: it will parse the args
160 as "ld.so PROGRAM [ARGS...]". For booting the Hurd, we
161 support an additional special syntax:
162 ld.so [-LIBS...] PROGRAM [ARGS...]
163 Each LIBS word consists of "FILENAME=MEMOBJ";
164 for example "-/lib/libc.so=123" says that the contents of
165 /lib/libc.so are found in a memory object whose port name
166 in our task is 123. */
167 while (_dl_argc > 2 && _dl_argv[1][0] == '-' && _dl_argv[1][1] != '-')
169 char *lastslash, *memobjname, *p;
170 struct link_map *l;
171 mach_port_t memobj;
172 error_t err;
174 ++_dl_skip_args;
175 --_dl_argc;
176 p = _dl_argv++[1] + 1;
178 memobjname = strchr (p, '=');
179 if (! memobjname)
180 _dl_sysdep_fatal ("Bogus library spec: ", p, "\n", NULL);
181 *memobjname++ = '\0';
182 memobj = 0;
183 while (*memobjname != '\0')
184 memobj = (memobj * 10) + (*memobjname++ - '0');
186 /* Add a user reference on the memory object port, so we will
187 still have one after _dl_map_object_from_fd calls our
188 `close'. */
189 err = __mach_port_mod_refs (__mach_task_self (), memobj,
190 MACH_PORT_RIGHT_SEND, +1);
191 assert_perror (err);
193 lastslash = strrchr (p, '/');
194 l = _dl_map_object_from_fd (lastslash ? lastslash + 1 : p, NULL,
195 memobj, strdup (p), 0);
197 /* Squirrel away the memory object port where it
198 can be retrieved by the program later. */
199 l->l_info[DT_NULL] = (void *) memobj;
201 #endif
203 /* Call elf/rtld.c's main program. It will set everything
204 up and leave us to transfer control to USER_ENTRY. */
205 (*dl_main) ((const ElfW(Phdr) *) _dl_hurd_data->phdr,
206 _dl_hurd_data->phdrsz / sizeof (ElfW(Phdr)),
207 (ElfW(Addr) *) &_dl_hurd_data->user_entry, NULL);
209 /* The call above might screw a few things up.
211 First of all, if _dl_skip_args is nonzero, we are ignoring
212 the first few arguments. However, if we have no Hurd startup
213 data, it is the magical convention that ARGV[0] == P. The
214 startup code in init-first.c will get confused if this is not
215 the case, so we must rearrange things to make it so. We'll
216 overwrite the origional ARGV[0] at P with ARGV[_dl_skip_args].
218 Secondly, if we need to be secure, it removes some dangerous
219 environment variables. If we have no Hurd startup date this
220 changes P (since that's the location after the terminating
221 NULL in the list of environment variables). We do the same
222 thing as in the first case but make sure we recalculate P.
223 If we do have Hurd startup data, we have to move the data
224 such that it starts just after the terminating NULL in the
225 environment list.
227 We use memmove, since the locations might overlap. */
228 if (__libc_enable_secure || _dl_skip_args)
230 char **newp;
232 for (newp = _environ; *newp++;);
234 if (_dl_argv[-_dl_skip_args] == (char *) p)
236 if ((char *) newp != _dl_argv[0])
238 assert ((char *) newp < _dl_argv[0]);
239 _dl_argv[0] = memmove ((char *) newp, _dl_argv[0],
240 strlen (_dl_argv[0]) + 1);
243 else
245 if ((void *) newp != _dl_hurd_data)
246 memmove (newp, _dl_hurd_data, sizeof (*_dl_hurd_data));
251 extern void _dl_start_user (void);
252 /* Unwind the stack to ARGDATA and simulate a return from _dl_start
253 to the RTLD_START code which will run the user's entry point. */
254 RETURN_TO (argdata, &_dl_start_user, _dl_hurd_data->user_entry);
258 /* Set up so we can do RPCs. */
259 __mach_init ();
261 /* Initialize frequently used global variable. */
262 GLRO(dl_pagesize) = __getpagesize ();
264 #if HP_TIMING_AVAIL
265 HP_TIMING_NOW (_dl_cpuclock_offset);
266 #endif
268 fmh(); /* XXX */
270 /* See hurd/hurdstartup.c; this deals with getting information
271 from the exec server and slicing up the arguments.
272 Then it will call `go', above. */
273 _hurd_startup (start_argptr, &go);
275 LOSE;
276 abort ();
279 void
280 _dl_sysdep_start_cleanup (void)
282 /* Deallocate the reply port and task port rights acquired by
283 __mach_init. We are done with them now, and the user will
284 reacquire them for himself when he wants them. */
285 __mig_dealloc_reply_port (MACH_PORT_NULL);
286 __mach_port_deallocate (__mach_task_self (), __mach_host_self_);
287 __mach_port_deallocate (__mach_task_self (), __mach_task_self_);
290 /* Minimal open/close/mmap implementation sufficient for initial loading of
291 shared libraries. These are weak definitions so that when the
292 dynamic linker re-relocates itself to be user-visible (for -ldl),
293 it will get the user's definition (i.e. usually libc's). */
295 /* This macro checks that the function does not get renamed to be hidden: we do
296 need these to be overridable by libc's. */
297 #define check_no_hidden(name) \
298 static void __check_##name##_no_hidden(void) __attribute__((alias(#name)));
300 /* Open FILE_NAME and return a Hurd I/O for it in *PORT, or return an
301 error. If STAT is non-zero, stat the file into that stat buffer. */
302 static error_t
303 open_file (const char *file_name, int flags,
304 mach_port_t *port, struct stat64 *stat)
306 enum retry_type doretry;
307 char retryname[1024]; /* XXX string_t LOSES! */
308 file_t startdir;
309 error_t err;
311 error_t use_init_port (int which, error_t (*operate) (file_t))
313 return (which < _dl_hurd_data->portarraysize
314 ? ((*operate) (_dl_hurd_data->portarray[which]))
315 : EGRATUITOUS);
317 file_t get_dtable_port (int fd)
319 if ((unsigned int) fd < _dl_hurd_data->dtablesize
320 && _dl_hurd_data->dtable[fd] != MACH_PORT_NULL)
322 __mach_port_mod_refs (__mach_task_self (), _dl_hurd_data->dtable[fd],
323 MACH_PORT_RIGHT_SEND, +1);
324 return _dl_hurd_data->dtable[fd];
326 errno = EBADF;
327 return MACH_PORT_NULL;
330 assert (!(flags & ~(O_READ | O_CLOEXEC)));
332 startdir = _dl_hurd_data->portarray[file_name[0] == '/' ?
333 INIT_PORT_CRDIR : INIT_PORT_CWDIR];
335 while (file_name[0] == '/')
336 file_name++;
338 err = __dir_lookup (startdir, (char *)file_name, O_RDONLY, 0,
339 &doretry, retryname, port);
341 if (!err)
342 err = __hurd_file_name_lookup_retry (use_init_port, get_dtable_port,
343 __dir_lookup, doretry, retryname,
344 O_RDONLY, 0, port);
345 if (!err && stat)
347 err = __io_stat (*port, stat);
348 if (err)
349 __mach_port_deallocate (__mach_task_self (), *port);
352 return err;
355 check_no_hidden(__open);
356 int weak_function
357 __open (const char *file_name, int mode, ...)
359 mach_port_t port;
360 error_t err = open_file (file_name, mode, &port, 0);
361 if (err)
362 return __hurd_fail (err);
363 else
364 return (int)port;
367 check_no_hidden(__close);
368 int weak_function
369 __close (int fd)
371 if (fd != (int) MACH_PORT_NULL)
372 __mach_port_deallocate (__mach_task_self (), (mach_port_t) fd);
373 return 0;
376 check_no_hidden(__libc_read);
377 __ssize_t weak_function
378 __libc_read (int fd, void *buf, size_t nbytes)
380 error_t err;
381 char *data;
382 mach_msg_type_number_t nread;
384 data = buf;
385 nread = nbytes;
386 err = __io_read ((mach_port_t) fd, &data, &nread, -1, nbytes);
387 if (err)
388 return __hurd_fail (err);
390 if (data != buf)
392 memcpy (buf, data, nread);
393 __vm_deallocate (__mach_task_self (), (vm_address_t) data, nread);
396 return nread;
398 libc_hidden_weak (__libc_read)
400 check_no_hidden(__libc_write);
401 __ssize_t weak_function
402 __libc_write (int fd, const void *buf, size_t nbytes)
404 error_t err;
405 mach_msg_type_number_t nwrote;
407 assert (fd < _hurd_init_dtablesize);
409 err = __io_write (_hurd_init_dtable[fd], buf, nbytes, -1, &nwrote);
410 if (err)
411 return __hurd_fail (err);
413 return nwrote;
415 libc_hidden_weak (__libc_write)
417 /* This is only used for printing messages (see dl-misc.c). */
418 check_no_hidden(__writev);
419 __ssize_t weak_function
420 __writev (int fd, const struct iovec *iov, int niov)
422 if (fd >= _hurd_init_dtablesize)
424 errno = EBADF;
425 return -1;
428 int i;
429 size_t total = 0;
430 for (i = 0; i < niov; ++i)
431 total += iov[i].iov_len;
433 if (total != 0)
435 char buf[total], *bufp = buf;
436 error_t err;
437 mach_msg_type_number_t nwrote;
439 for (i = 0; i < niov; ++i)
440 bufp = (memcpy (bufp, iov[i].iov_base, iov[i].iov_len)
441 + iov[i].iov_len);
443 err = __io_write (_hurd_init_dtable[fd], buf, total, -1, &nwrote);
444 if (err)
445 return __hurd_fail (err);
447 return nwrote;
449 return 0;
452 check_no_hidden(__libc_lseek64);
453 off64_t weak_function
454 __libc_lseek64 (int fd, off64_t offset, int whence)
456 error_t err;
458 err = __io_seek ((mach_port_t) fd, offset, whence, &offset);
459 if (err)
460 return __hurd_fail (err);
462 return offset;
465 check_no_hidden(__mmap);
466 void *weak_function
467 __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
469 error_t err;
470 vm_prot_t vmprot;
471 vm_address_t mapaddr;
472 mach_port_t memobj_rd, memobj_wr;
474 vmprot = VM_PROT_NONE;
475 if (prot & PROT_READ)
476 vmprot |= VM_PROT_READ;
477 if (prot & PROT_WRITE)
478 vmprot |= VM_PROT_WRITE;
479 if (prot & PROT_EXEC)
480 vmprot |= VM_PROT_EXECUTE;
482 if (flags & MAP_ANON)
483 memobj_rd = MACH_PORT_NULL;
484 else
486 assert (!(flags & MAP_SHARED));
487 err = __io_map ((mach_port_t) fd, &memobj_rd, &memobj_wr);
488 if (err)
489 return __hurd_fail (err), MAP_FAILED;
490 if (memobj_wr != MACH_PORT_NULL)
491 __mach_port_deallocate (__mach_task_self (), memobj_wr);
494 mapaddr = (vm_address_t) addr;
495 err = __vm_map (__mach_task_self (),
496 &mapaddr, (vm_size_t) len, ELF_MACHINE_USER_ADDRESS_MASK,
497 !(flags & MAP_FIXED),
498 memobj_rd,
499 (vm_offset_t) offset,
500 flags & (MAP_COPY|MAP_PRIVATE),
501 vmprot, VM_PROT_ALL,
502 (flags & MAP_SHARED) ? VM_INHERIT_SHARE : VM_INHERIT_COPY);
503 if (err == KERN_NO_SPACE && (flags & MAP_FIXED))
505 /* XXX this is not atomic as it is in unix! */
506 /* The region is already allocated; deallocate it first. */
507 err = __vm_deallocate (__mach_task_self (), mapaddr, len);
508 if (! err)
509 err = __vm_map (__mach_task_self (),
510 &mapaddr, (vm_size_t) len,
511 ELF_MACHINE_USER_ADDRESS_MASK,
512 !(flags & MAP_FIXED),
513 memobj_rd, (vm_offset_t) offset,
514 flags & (MAP_COPY|MAP_PRIVATE),
515 vmprot, VM_PROT_ALL,
516 (flags & MAP_SHARED)
517 ? VM_INHERIT_SHARE : VM_INHERIT_COPY);
520 if ((flags & MAP_ANON) == 0)
521 __mach_port_deallocate (__mach_task_self (), memobj_rd);
523 if (err)
524 return __hurd_fail (err), MAP_FAILED;
525 return (void *) mapaddr;
528 check_no_hidden(__fxstat64);
529 int weak_function
530 __fxstat64 (int vers, int fd, struct stat64 *buf)
532 error_t err;
534 assert (vers == _STAT_VER);
536 err = __io_stat ((mach_port_t) fd, buf);
537 if (err)
538 return __hurd_fail (err);
540 return 0;
542 libc_hidden_def (__fxstat64)
544 check_no_hidden(__xstat64);
545 int weak_function
546 __xstat64 (int vers, const char *file, struct stat64 *buf)
548 error_t err;
549 mach_port_t port;
551 assert (vers == _STAT_VER);
553 err = open_file (file, 0, &port, buf);
554 if (err)
555 return __hurd_fail (err);
557 __mach_port_deallocate (__mach_task_self (), port);
559 return 0;
561 libc_hidden_def (__xstat64)
563 /* This function is called by the dynamic linker (rtld.c) to check
564 whether debugging malloc is allowed even for SUID binaries. This
565 stub will always fail, which means that malloc-debugging is always
566 disabled for SUID binaries. */
567 check_no_hidden(__access);
568 int weak_function
569 __access (const char *file, int type)
571 errno = ENOSYS;
572 return -1;
574 check_no_hidden(__access_noerrno);
575 int weak_function
576 __access_noerrno (const char *file, int type)
578 return -1;
581 check_no_hidden(__getpid);
582 pid_t weak_function
583 __getpid (void)
585 pid_t pid, ppid;
586 int orphaned;
588 if (__proc_getpids (_dl_hurd_data->portarray[INIT_PORT_PROC],
589 &pid, &ppid, &orphaned))
590 return -1;
592 return pid;
595 /* We need this alias to satisfy references from libc_pic.a objects
596 that were affected by the libc_hidden_proto declaration for __getpid. */
597 strong_alias (__getpid, __GI___getpid)
599 /* This is called only in some strange cases trying to guess a value
600 for $ORIGIN for the executable. The dynamic linker copes with
601 getcwd failing (dl-object.c), and it's too much hassle to include
602 the functionality here. (We could, it just requires duplicating or
603 reusing getcwd.c's code but using our special lookup function as in
604 `open', above.) */
605 check_no_hidden(__getcwd);
606 char *weak_function
607 __getcwd (char *buf, size_t size)
609 errno = ENOSYS;
610 return NULL;
613 /* This is used by dl-tunables.c to strdup strings. We can just make this a
614 mere allocation. */
615 check_no_hidden(__sbrk);
616 void *weak_function
617 __sbrk (intptr_t increment)
619 vm_address_t addr;
620 __vm_allocate (__mach_task_self (), &addr, increment, 1);
621 return (void *) addr;
624 check_no_hidden(__strtoul_internal);
625 unsigned long int weak_function
626 __strtoul_internal (const char *nptr, char **endptr, int base, int group)
628 assert (base == 0 || base == 10);
629 assert (group == 0);
630 return _dl_strtoul (nptr, endptr);
633 /* We need this alias to satisfy references from libc_pic.a objects
634 that were affected by the libc_hidden_proto declaration for __strtoul_internal. */
635 strong_alias (__strtoul_internal, __GI___strtoul_internal)
636 strong_alias (__strtoul_internal, __GI_____strtoul_internal)
638 check_no_hidden(_exit);
639 void weak_function attribute_hidden
640 _exit (int status)
642 __proc_mark_exit (_dl_hurd_data->portarray[INIT_PORT_PROC],
643 W_EXITCODE (status, 0), 0);
644 while (__task_terminate (__mach_task_self ()))
645 __mach_task_self_ = (__mach_task_self) ();
647 LOSE;
648 abort ();
650 /* We need this alias to satisfy references from libc_pic.a objects
651 that were affected by the libc_hidden_proto declaration for _exit. */
652 strong_alias (_exit, __GI__exit)
654 /* Try to get a machine dependent instruction which will make the
655 program crash. This is used in case everything else fails. */
656 #include <abort-instr.h>
657 #ifndef ABORT_INSTRUCTION
658 /* No such instruction is available. */
659 # define ABORT_INSTRUCTION
660 #endif
662 check_no_hidden(abort);
663 void weak_function
664 abort (void)
666 /* Try to abort using the system specific command. */
667 ABORT_INSTRUCTION;
669 /* If the abort instruction failed, exit. */
670 _exit (127);
672 /* If even this fails, make sure we never return. */
673 while (1)
674 /* Try for ever and ever. */
675 ABORT_INSTRUCTION;
678 /* We need this alias to satisfy references from libc_pic.a objects
679 that were affected by the libc_hidden_proto declaration for abort. */
680 strong_alias (abort, __GI_abort)
681 strong_alias (abort, __GI___chk_fail)
682 strong_alias (abort, __GI___fortify_fail)
683 strong_alias (abort, __GI___assert_fail)
684 strong_alias (abort, __GI___assert_perror_fail)
686 /* This function is called by interruptible RPC stubs. For initial
687 dynamic linking, just use the normal mach_msg. Since this defn is
688 weak, the real defn in libc.so will override it if we are linked into
689 the user program (-ldl). */
691 error_t weak_function
692 _hurd_intr_rpc_mach_msg (mach_msg_header_t *msg,
693 mach_msg_option_t option,
694 mach_msg_size_t send_size,
695 mach_msg_size_t rcv_size,
696 mach_port_t rcv_name,
697 mach_msg_timeout_t timeout,
698 mach_port_t notify)
700 return __mach_msg (msg, option, send_size, rcv_size, rcv_name,
701 timeout, notify);
705 void
706 _dl_show_auxv (void)
708 /* There is nothing to print. Hurd has no auxiliary vector. */
712 void weak_function
713 _dl_init_first (int argc, ...)
715 /* This no-op definition only gets used if libc is not linked in. */
718 #endif /* SHARED */