]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mach/hurd/dl-sysdep.c
2003-12-21 Roland McGrath <roland@frob.com>
[thirdparty/glibc.git] / sysdeps / mach / hurd / dl-sysdep.c
1 /* Operating system support for run-time dynamic linker. Hurd version.
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5
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.
10
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.
15
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. */
20
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>
40
41 #include <entry.h>
42 #include <dl-machine.h>
43 #include <dl-procinfo.h>
44
45 extern void __mach_init (void);
46
47 extern int _dl_argc;
48 extern char **_dl_argv;
49 extern char **_environ;
50
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;
57
58 #if HP_TIMING_AVAIL
59 hp_timing_t _dl_cpuclock_offset;
60 #endif
61
62
63 struct hurd_startup_data *_dl_hurd_data;
64
65 /* This is used only within ld.so, via dl-minimal.c's __errno_location. */
66 #undef errno
67 int errno attribute_hidden;
68
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;
73
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;
83
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);}
111 }
112 /* XXX loser kludge for vm_map kernel bug */
113 #endif
114
115
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))
120 {
121 void go (intptr_t *argdata)
122 {
123 extern unsigned int _dl_skip_args; /* rtld.c */
124 char **p;
125
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. */
131
132 if ((void *) p == _dl_argv[0])
133 {
134 static struct hurd_startup_data nodata;
135 _dl_hurd_data = &nodata;
136 nodata.user_entry = (vm_address_t) ENTRY_POINT;
137 }
138 else
139 _dl_hurd_data = (void *) p;
140
141 INTUSE(__libc_enable_secure) = _dl_hurd_data->flags & EXEC_SECURE;
142
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;
146
147 unfmh(); /* XXX */
148
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] != '-')
161 {
162 char *lastslash, *memobjname, *p;
163 struct link_map *l;
164 mach_port_t memobj;
165 error_t err;
166
167 ++_dl_skip_args;
168 --_dl_argc;
169 p = _dl_argv++[1] + 1;
170
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');
178
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);
185
186 lastslash = strrchr (p, '/');
187 l = _dl_map_object_from_fd (lastslash ? lastslash + 1 : p,
188 memobj, strdup (p), 0);
189
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;
193 }
194 #endif
195
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);
201
202 /* The call above might screw a few things up.
203
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].
210
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.
219
220 We use memmove, since the locations might overlap. */
221 if (INTUSE(__libc_enable_secure) || _dl_skip_args)
222 {
223 char **newp;
224
225 for (newp = _environ; *newp++;);
226
227 if (_dl_argv[-_dl_skip_args] == (char *) p)
228 {
229 if ((char *) newp != _dl_argv[0])
230 {
231 assert ((char *) newp < _dl_argv[0]);
232 _dl_argv[0] = memmove ((char *) newp, _dl_argv[0],
233 strlen (_dl_argv[0]) + 1);
234 }
235 }
236 else
237 {
238 if ((void *) newp != _dl_hurd_data)
239 memmove (newp, _dl_hurd_data, sizeof (*_dl_hurd_data));
240 }
241 }
242
243 {
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);
248 }
249 }
250
251 /* Set up so we can do RPCs. */
252 __mach_init ();
253
254 /* Initialize frequently used global variable. */
255 GL(dl_pagesize) = __getpagesize ();
256
257 #if HP_TIMING_AVAIL
258 HP_TIMING_NOW (_dl_cpuclock_offset);
259 #endif
260
261 fmh(); /* XXX */
262
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);
267
268 LOSE;
269 abort ();
270 }
271
272 void
273 internal_function
274 _dl_sysdep_start_cleanup (void)
275 {
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_);
281 }
282 \f
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). */
287
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)
293 {
294 enum retry_type doretry;
295 char retryname[1024]; /* XXX string_t LOSES! */
296 file_t startdir;
297 error_t err;
298
299 error_t use_init_port (int which, error_t (*operate) (file_t))
300 {
301 return (which < _dl_hurd_data->portarraysize
302 ? ((*operate) (_dl_hurd_data->portarray[which]))
303 : EGRATUITOUS);
304 }
305 file_t get_dtable_port (int fd)
306 {
307 if ((unsigned int) fd < _dl_hurd_data->dtablesize
308 && _dl_hurd_data->dtable[fd] != MACH_PORT_NULL)
309 {
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];
313 }
314 errno = EBADF;
315 return MACH_PORT_NULL;
316 }
317
318 assert (!(flags & ~O_READ));
319
320 startdir = _dl_hurd_data->portarray[file_name[0] == '/' ?
321 INIT_PORT_CRDIR : INIT_PORT_CWDIR];
322
323 while (file_name[0] == '/')
324 file_name++;
325
326 err = __dir_lookup (startdir, (char *)file_name, O_RDONLY, 0,
327 &doretry, retryname, port);
328
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)
334 {
335 err = __io_stat (*port, stat);
336 if (err)
337 __mach_port_deallocate (__mach_task_self (), *port);
338 }
339
340 return err;
341 }
342
343 int weak_function
344 __open (const char *file_name, int mode, ...)
345 {
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;
352 }
353
354 int weak_function
355 __close (int fd)
356 {
357 if (fd != (int) MACH_PORT_NULL)
358 __mach_port_deallocate (__mach_task_self (), (mach_port_t) fd);
359 return 0;
360 }
361
362 __ssize_t weak_function
363 __libc_read (int fd, void *buf, size_t nbytes)
364 {
365 error_t err;
366 char *data;
367 mach_msg_type_number_t nread;
368
369 data = buf;
370 err = __io_read ((mach_port_t) fd, &data, &nread, -1, nbytes);
371 if (err)
372 return __hurd_fail (err);
373
374 if (data != buf)
375 {
376 memcpy (buf, data, nread);
377 __vm_deallocate (__mach_task_self (), (vm_address_t) data, nread);
378 }
379
380 return nread;
381 }
382 libc_hidden_weak (__libc_read)
383
384 __ssize_t weak_function
385 __libc_write (int fd, const void *buf, size_t nbytes)
386 {
387 error_t err;
388 mach_msg_type_number_t nwrote;
389
390 assert (fd < _hurd_init_dtablesize);
391
392 err = __io_write (_hurd_init_dtable[fd], buf, nbytes, -1, &nwrote);
393 if (err)
394 return __hurd_fail (err);
395
396 return nwrote;
397 }
398 libc_hidden_weak (__libc_write)
399
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)
403 {
404 int i;
405 size_t total = 0;
406 for (i = 0; i < niov; ++i)
407 total += iov[i].iov_len;
408
409 assert (fd < _hurd_init_dtablesize);
410
411 if (total != 0)
412 {
413 char buf[total], *bufp = buf;
414 error_t err;
415 mach_msg_type_number_t nwrote;
416
417 for (i = 0; i < niov; ++i)
418 bufp = (memcpy (bufp, iov[i].iov_base, iov[i].iov_len)
419 + iov[i].iov_len);
420
421 err = __io_write (_hurd_init_dtable[fd], buf, total, -1, &nwrote);
422 if (err)
423 return __hurd_fail (err);
424
425 return nwrote;
426 }
427 return 0;
428 }
429
430
431 off64_t weak_function
432 __libc_lseek64 (int fd, off64_t offset, int whence)
433 {
434 error_t err;
435
436 err = __io_seek ((mach_port_t) fd, offset, whence, &offset);
437 if (err)
438 return __hurd_fail (err);
439
440 return offset;
441 }
442
443 __ptr_t weak_function
444 __mmap (__ptr_t addr, size_t len, int prot, int flags, int fd, off_t offset)
445 {
446 error_t err;
447 vm_prot_t vmprot;
448 vm_address_t mapaddr;
449 mach_port_t memobj_rd, memobj_wr;
450
451 vmprot = VM_PROT_NONE;
452 if (prot & PROT_READ)
453 vmprot |= VM_PROT_READ;
454 if (prot & PROT_WRITE)
455 vmprot |= VM_PROT_WRITE;
456 if (prot & PROT_EXEC)
457 vmprot |= VM_PROT_EXECUTE;
458
459 if (flags & MAP_ANON)
460 memobj_rd = MACH_PORT_NULL;
461 else
462 {
463 assert (!(flags & MAP_SHARED));
464 err = __io_map ((mach_port_t) fd, &memobj_rd, &memobj_wr);
465 if (err)
466 return __hurd_fail (err), MAP_FAILED;
467 __mach_port_deallocate (__mach_task_self (), memobj_wr);
468 }
469
470 mapaddr = (vm_address_t) addr;
471 err = __vm_map (__mach_task_self (),
472 &mapaddr, (vm_size_t) len, ELF_MACHINE_USER_ADDRESS_MASK,
473 !(flags & MAP_FIXED),
474 memobj_rd,
475 (vm_offset_t) offset,
476 flags & (MAP_COPY|MAP_PRIVATE),
477 vmprot, VM_PROT_ALL,
478 (flags & MAP_SHARED) ? VM_INHERIT_SHARE : VM_INHERIT_COPY);
479 if (err == KERN_NO_SPACE && (flags & MAP_FIXED))
480 {
481 /* XXX this is not atomic as it is in unix! */
482 /* The region is already allocated; deallocate it first. */
483 err = __vm_deallocate (__mach_task_self (), mapaddr, len);
484 if (! err)
485 err = __vm_map (__mach_task_self (),
486 &mapaddr, (vm_size_t) len,
487 ELF_MACHINE_USER_ADDRESS_MASK,
488 !(flags & MAP_FIXED),
489 memobj_rd, (vm_offset_t) offset,
490 flags & (MAP_COPY|MAP_PRIVATE),
491 vmprot, VM_PROT_ALL,
492 (flags & MAP_SHARED)
493 ? VM_INHERIT_SHARE : VM_INHERIT_COPY);
494 }
495
496 if ((flags & MAP_ANON) == 0)
497 __mach_port_deallocate (__mach_task_self (), memobj_rd);
498
499 if (err)
500 return __hurd_fail (err), MAP_FAILED;
501 return (__ptr_t) mapaddr;
502 }
503
504 int weak_function
505 __fxstat64 (int vers, int fd, struct stat64 *buf)
506 {
507 error_t err;
508
509 assert (vers == _STAT_VER);
510
511 err = __io_stat ((mach_port_t) fd, buf);
512 if (err)
513 return __hurd_fail (err);
514
515 return 0;
516 }
517 libc_hidden_def (__fxstat64)
518
519 int weak_function
520 __xstat64 (int vers, const char *file, struct stat64 *buf)
521 {
522 error_t err;
523 mach_port_t port;
524
525 assert (vers == _STAT_VER);
526
527 err = open_file (file, 0, &port, buf);
528 if (err)
529 return __hurd_fail (err);
530
531 __mach_port_deallocate (__mach_task_self (), port);
532
533 return 0;
534 }
535 libc_hidden_def (__xstat64)
536
537 /* This function is called by the dynamic linker (rtld.c) to check
538 whether debugging malloc is allowed even for SUID binaries. This
539 stub will always fail, which means that malloc-debugging is always
540 disabled for SUID binaries. */
541 int weak_function
542 __access (const char *file, int type)
543 {
544 errno = ENOSYS;
545 return -1;
546 }
547
548 pid_t weak_function
549 __getpid ()
550 {
551 pid_t pid, ppid;
552 int orphaned;
553
554 if (__proc_getpids (_dl_hurd_data->portarray[INIT_PORT_PROC],
555 &pid, &ppid, &orphaned))
556 return -1;
557
558 return pid;
559 }
560
561 /* This is called only in some strange cases trying to guess a value
562 for $ORIGIN for the executable. The dynamic linker copes with
563 getcwd failing (dl-object.c), and it's too much hassle to include
564 the functionality here. (We could, it just requires duplicating or
565 reusing getcwd.c's code but using our special lookup function as in
566 `open', above.) */
567 char *
568 weak_function
569 __getcwd (char *buf, size_t size)
570 {
571 errno = ENOSYS;
572 return NULL;
573 }
574
575 void weak_function attribute_hidden
576 _exit (int status)
577 {
578 __proc_mark_exit (_dl_hurd_data->portarray[INIT_PORT_PROC],
579 W_EXITCODE (status, 0), 0);
580 while (__task_terminate (__mach_task_self ()))
581 __mach_task_self_ = (__mach_task_self) ();
582 }
583 /* We need this alias to satisfy references from libc_pic.a objects
584 that were affected by the libc_hidden_proto declaration for _exit. */
585 strong_alias (_exit, __GI__exit)
586
587 /* Try to get a machine dependent instruction which will make the
588 program crash. This is used in case everything else fails. */
589 #include <abort-instr.h>
590 #ifndef ABORT_INSTRUCTION
591 /* No such instruction is available. */
592 # define ABORT_INSTRUCTION
593 #endif
594
595 void weak_function
596 abort (void)
597 {
598 /* Try to abort using the system specific command. */
599 ABORT_INSTRUCTION;
600
601 /* If the abort instruction failed, exit. */
602 _exit (127);
603
604 /* If even this fails, make sure we never return. */
605 while (1)
606 /* Try for ever and ever. */
607 ABORT_INSTRUCTION;
608 }
609
610 /* We need this alias to satisfy references from libc_pic.a objects
611 that were affected by the libc_hidden_proto declaration for abort. */
612 strong_alias (abort, __GI_abort)
613 \f
614 /* This function is called by interruptible RPC stubs. For initial
615 dynamic linking, just use the normal mach_msg. Since this defn is
616 weak, the real defn in libc.so will override it if we are linked into
617 the user program (-ldl). */
618
619 error_t weak_function
620 _hurd_intr_rpc_mach_msg (mach_msg_header_t *msg,
621 mach_msg_option_t option,
622 mach_msg_size_t send_size,
623 mach_msg_size_t rcv_size,
624 mach_port_t rcv_name,
625 mach_msg_timeout_t timeout,
626 mach_port_t notify)
627 {
628 return __mach_msg (msg, option, send_size, rcv_size, rcv_name,
629 timeout, notify);
630 }
631
632
633 void
634 internal_function
635 _dl_show_auxv (void)
636 {
637 /* There is nothing to print. Hurd has no auxiliary vector. */
638 }
639
640
641 /* Return an array of useful/necessary hardware capability names. */
642 const struct r_strlenpair *
643 internal_function
644 _dl_important_hwcaps (const char *platform, size_t platform_len, size_t *sz,
645 size_t *max_capstrlen)
646 {
647 struct r_strlenpair *result;
648
649 /* Return an empty array. Hurd has no hardware capabilities. */
650 result = (struct r_strlenpair *) malloc (sizeof (*result));
651 if (result == NULL)
652 INTUSE (_dl_signal_error) (ENOMEM, NULL, NULL,
653 "cannot create capability list");
654
655 result[0].str = (char *) result; /* Does not really matter. */
656 result[0].len = 0;
657
658 *sz = 1;
659 return result;
660 }
661
662 void weak_function
663 _dl_init_first (int argc, ...)
664 {
665 /* This no-op definition only gets used if libc is not linked in. */
666 }