Updated to fedora-glibc-20080703T1203
[glibc.git] / sysdeps / mach / hurd / dl-sysdep.c
blob244068290349ecc0af35f4b2208e61388a16f18e
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
19 02111-1307 USA. */
21 #include <hurd.h>
22 #include <link.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <sys/mman.h>
27 #include <ldsodefs.h>
28 #include <sys/wait.h>
29 #include <assert.h>
30 #include <sysdep.h>
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>
36 #include <stdarg.h>
37 #include <ctype.h>
38 #include <sys/stat.h>
39 #include <sys/uio.h>
41 #include <entry.h>
42 #include <dl-machine.h>
43 #include <dl-procinfo.h>
45 extern void __mach_init (void);
47 extern int _dl_argc;
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
54 of init-first. */
55 /* This variable containts the lowest stack address ever used. */
56 void *__libc_stack_end;
58 #if HP_TIMING_AVAIL
59 hp_timing_t _dl_cpuclock_offset;
60 #endif
63 struct hurd_startup_data *_dl_hurd_data;
65 /* This is used only within ld.so, via dl-minimal.c's __errno_location. */
66 #undef errno
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__)
85 #if ! FMH
86 # define fmh() ((void)0)
87 # define unfmh() ((void)0)
88 #else
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){
102 max=a; break;}
103 fmha=a+=fmhs;}
104 if (err) assert(err==KERN_NO_SPACE);
105 if (!fmha)fmhs=0;else{
106 fmhs=max-fmha;
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);
110 assert_perror(err);}
112 /* XXX loser kludge for vm_map kernel bug */
113 #endif
116 ElfW(Addr)
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 */
124 char **p;
126 /* Cache the information in various global variables. */
127 _dl_argc = *argdata;
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;
138 else
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;
147 unfmh(); /* XXX */
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;
163 struct link_map *l;
164 mach_port_t memobj;
165 error_t err;
167 ++_dl_skip_args;
168 --_dl_argc;
169 p = _dl_argv++[1] + 1;
171 memobjname = strchr (p, '=');
172 if (! memobjname)
173 _dl_sysdep_fatal ("Bogus library spec: ", p, "\n", NULL);
174 *memobjname++ = '\0';
175 memobj = 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
181 `close'. */
182 err = __mach_port_mod_refs (__mach_task_self (), memobj,
183 MACH_PORT_RIGHT_SEND, +1);
184 assert_perror (err);
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;
194 #endif
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
218 environment list.
220 We use memmove, since the locations might overlap. */
221 if (INTUSE(__libc_enable_secure) || _dl_skip_args)
223 char **newp;
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);
236 else
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. */
252 __mach_init ();
254 /* Initialize frequently used global variable. */
255 GLRO(dl_pagesize) = __getpagesize ();
257 #if HP_TIMING_AVAIL
258 HP_TIMING_NOW (_dl_cpuclock_offset);
259 #endif
261 fmh(); /* XXX */
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);
268 LOSE;
269 abort ();
272 void
273 internal_function
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. */
290 static error_t
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! */
296 file_t startdir;
297 error_t err;
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]))
303 : EGRATUITOUS);
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];
314 errno = EBADF;
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] == '/')
324 file_name++;
326 err = __dir_lookup (startdir, (char *)file_name, O_RDONLY, 0,
327 &doretry, retryname, port);
329 if (!err)
330 err = __hurd_file_name_lookup_retry (use_init_port, get_dtable_port,
331 __dir_lookup, doretry, retryname,
332 O_RDONLY, 0, port);
333 if (!err && stat)
335 err = __io_stat (*port, stat);
336 if (err)
337 __mach_port_deallocate (__mach_task_self (), *port);
340 return err;
343 int weak_function
344 __open (const char *file_name, int mode, ...)
346 mach_port_t port;
347 error_t err = open_file (file_name, mode, &port, 0);
348 if (err)
349 return __hurd_fail (err);
350 else
351 return (int)port;
354 int weak_function
355 __close (int fd)
357 if (fd != (int) MACH_PORT_NULL)
358 __mach_port_deallocate (__mach_task_self (), (mach_port_t) fd);
359 return 0;
362 __ssize_t weak_function
363 __libc_read (int fd, void *buf, size_t nbytes)
365 error_t err;
366 char *data;
367 mach_msg_type_number_t nread;
369 data = buf;
370 nread = nbytes;
371 err = __io_read ((mach_port_t) fd, &data, &nread, -1, nbytes);
372 if (err)
373 return __hurd_fail (err);
375 if (data != buf)
377 memcpy (buf, data, nread);
378 __vm_deallocate (__mach_task_self (), (vm_address_t) data, nread);
381 return nread;
383 libc_hidden_weak (__libc_read)
385 __ssize_t weak_function
386 __libc_write (int fd, const void *buf, size_t nbytes)
388 error_t err;
389 mach_msg_type_number_t nwrote;
391 assert (fd < _hurd_init_dtablesize);
393 err = __io_write (_hurd_init_dtable[fd], buf, nbytes, -1, &nwrote);
394 if (err)
395 return __hurd_fail (err);
397 return nwrote;
399 libc_hidden_weak (__libc_write)
401 /* This is only used for printing messages (see dl-misc.c). */
402 __ssize_t weak_function
403 __writev (int fd, const struct iovec *iov, int niov)
405 if (fd >= _hurd_init_dtablesize)
407 errno = EBADF;
408 return -1;
411 int i;
412 size_t total = 0;
413 for (i = 0; i < niov; ++i)
414 total += iov[i].iov_len;
416 if (total != 0)
418 char buf[total], *bufp = buf;
419 error_t err;
420 mach_msg_type_number_t nwrote;
422 for (i = 0; i < niov; ++i)
423 bufp = (memcpy (bufp, iov[i].iov_base, iov[i].iov_len)
424 + iov[i].iov_len);
426 err = __io_write (_hurd_init_dtable[fd], buf, total, -1, &nwrote);
427 if (err)
428 return __hurd_fail (err);
430 return nwrote;
432 return 0;
436 off64_t weak_function
437 __libc_lseek64 (int fd, off64_t offset, int whence)
439 error_t err;
441 err = __io_seek ((mach_port_t) fd, offset, whence, &offset);
442 if (err)
443 return __hurd_fail (err);
445 return offset;
448 __ptr_t weak_function
449 __mmap (__ptr_t addr, size_t len, int prot, int flags, int fd, off_t offset)
451 error_t err;
452 vm_prot_t vmprot;
453 vm_address_t mapaddr;
454 mach_port_t memobj_rd, memobj_wr;
456 vmprot = VM_PROT_NONE;
457 if (prot & PROT_READ)
458 vmprot |= VM_PROT_READ;
459 if (prot & PROT_WRITE)
460 vmprot |= VM_PROT_WRITE;
461 if (prot & PROT_EXEC)
462 vmprot |= VM_PROT_EXECUTE;
464 if (flags & MAP_ANON)
465 memobj_rd = MACH_PORT_NULL;
466 else
468 assert (!(flags & MAP_SHARED));
469 err = __io_map ((mach_port_t) fd, &memobj_rd, &memobj_wr);
470 if (err)
471 return __hurd_fail (err), MAP_FAILED;
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, ELF_MACHINE_USER_ADDRESS_MASK,
478 !(flags & MAP_FIXED),
479 memobj_rd,
480 (vm_offset_t) offset,
481 flags & (MAP_COPY|MAP_PRIVATE),
482 vmprot, VM_PROT_ALL,
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);
489 if (! err)
490 err = __vm_map (__mach_task_self (),
491 &mapaddr, (vm_size_t) len,
492 ELF_MACHINE_USER_ADDRESS_MASK,
493 !(flags & MAP_FIXED),
494 memobj_rd, (vm_offset_t) offset,
495 flags & (MAP_COPY|MAP_PRIVATE),
496 vmprot, VM_PROT_ALL,
497 (flags & MAP_SHARED)
498 ? VM_INHERIT_SHARE : VM_INHERIT_COPY);
501 if ((flags & MAP_ANON) == 0)
502 __mach_port_deallocate (__mach_task_self (), memobj_rd);
504 if (err)
505 return __hurd_fail (err), MAP_FAILED;
506 return (__ptr_t) mapaddr;
509 int weak_function
510 __fxstat64 (int vers, int fd, struct stat64 *buf)
512 error_t err;
514 assert (vers == _STAT_VER);
516 err = __io_stat ((mach_port_t) fd, buf);
517 if (err)
518 return __hurd_fail (err);
520 return 0;
522 libc_hidden_def (__fxstat64)
524 int weak_function
525 __xstat64 (int vers, const char *file, struct stat64 *buf)
527 error_t err;
528 mach_port_t port;
530 assert (vers == _STAT_VER);
532 err = open_file (file, 0, &port, buf);
533 if (err)
534 return __hurd_fail (err);
536 __mach_port_deallocate (__mach_task_self (), port);
538 return 0;
540 libc_hidden_def (__xstat64)
542 /* This function is called by the dynamic linker (rtld.c) to check
543 whether debugging malloc is allowed even for SUID binaries. This
544 stub will always fail, which means that malloc-debugging is always
545 disabled for SUID binaries. */
546 int weak_function
547 __access (const char *file, int type)
549 errno = ENOSYS;
550 return -1;
553 pid_t weak_function
554 __getpid ()
556 pid_t pid, ppid;
557 int orphaned;
559 if (__proc_getpids (_dl_hurd_data->portarray[INIT_PORT_PROC],
560 &pid, &ppid, &orphaned))
561 return -1;
563 return pid;
566 /* This is called only in some strange cases trying to guess a value
567 for $ORIGIN for the executable. The dynamic linker copes with
568 getcwd failing (dl-object.c), and it's too much hassle to include
569 the functionality here. (We could, it just requires duplicating or
570 reusing getcwd.c's code but using our special lookup function as in
571 `open', above.) */
572 char *
573 weak_function
574 __getcwd (char *buf, size_t size)
576 errno = ENOSYS;
577 return NULL;
580 void weak_function attribute_hidden
581 _exit (int status)
583 __proc_mark_exit (_dl_hurd_data->portarray[INIT_PORT_PROC],
584 W_EXITCODE (status, 0), 0);
585 while (__task_terminate (__mach_task_self ()))
586 __mach_task_self_ = (__mach_task_self) ();
588 /* We need this alias to satisfy references from libc_pic.a objects
589 that were affected by the libc_hidden_proto declaration for _exit. */
590 strong_alias (_exit, __GI__exit)
592 /* Try to get a machine dependent instruction which will make the
593 program crash. This is used in case everything else fails. */
594 #include <abort-instr.h>
595 #ifndef ABORT_INSTRUCTION
596 /* No such instruction is available. */
597 # define ABORT_INSTRUCTION
598 #endif
600 void weak_function
601 abort (void)
603 /* Try to abort using the system specific command. */
604 ABORT_INSTRUCTION;
606 /* If the abort instruction failed, exit. */
607 _exit (127);
609 /* If even this fails, make sure we never return. */
610 while (1)
611 /* Try for ever and ever. */
612 ABORT_INSTRUCTION;
615 /* We need this alias to satisfy references from libc_pic.a objects
616 that were affected by the libc_hidden_proto declaration for abort. */
617 strong_alias (abort, __GI_abort)
619 /* This function is called by interruptible RPC stubs. For initial
620 dynamic linking, just use the normal mach_msg. Since this defn is
621 weak, the real defn in libc.so will override it if we are linked into
622 the user program (-ldl). */
624 error_t weak_function
625 _hurd_intr_rpc_mach_msg (mach_msg_header_t *msg,
626 mach_msg_option_t option,
627 mach_msg_size_t send_size,
628 mach_msg_size_t rcv_size,
629 mach_port_t rcv_name,
630 mach_msg_timeout_t timeout,
631 mach_port_t notify)
633 return __mach_msg (msg, option, send_size, rcv_size, rcv_name,
634 timeout, notify);
638 void
639 internal_function
640 _dl_show_auxv (void)
642 /* There is nothing to print. Hurd has no auxiliary vector. */
646 /* Return an array of useful/necessary hardware capability names. */
647 const struct r_strlenpair *
648 internal_function
649 _dl_important_hwcaps (const char *platform, size_t platform_len, size_t *sz,
650 size_t *max_capstrlen)
652 struct r_strlenpair *result;
654 /* Return an empty array. Hurd has no hardware capabilities. */
655 result = (struct r_strlenpair *) malloc (sizeof (*result));
656 if (result == NULL)
657 _dl_signal_error (ENOMEM, NULL, NULL, "cannot create capability list");
659 result[0].str = (char *) result; /* Does not really matter. */
660 result[0].len = 0;
662 *sz = 1;
663 return result;
666 void weak_function
667 _dl_init_first (int argc, ...)
669 /* This no-op definition only gets used if libc is not linked in. */