]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mach/hurd/dl-sysdep.c
Hurd: O_CLOEXEC in rtld
[thirdparty/glibc.git] / sysdeps / mach / hurd / dl-sysdep.c
1 /* Operating system support for run-time dynamic linker. Hurd version.
2 Copyright (C) 1995-2012 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
19 #include <hurd.h>
20 #include <link.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <stdlib.h>
24 #include <sys/mman.h>
25 #include <ldsodefs.h>
26 #include <sys/wait.h>
27 #include <assert.h>
28 #include <sysdep.h>
29 #include <mach/mig_support.h>
30 #include "hurdstartup.h"
31 #include <hurd/lookup.h>
32 #include <hurd/auth.h>
33 #include <hurd/term.h>
34 #include <stdarg.h>
35 #include <ctype.h>
36 #include <sys/stat.h>
37 #include <sys/uio.h>
38
39 #include <entry.h>
40 #include <dl-machine.h>
41 #include <dl-procinfo.h>
42
43 extern void __mach_init (void);
44
45 extern int _dl_argc;
46 extern char **_dl_argv;
47 extern char **_environ;
48
49 int __libc_enable_secure = 0;
50 INTVARDEF(__libc_enable_secure)
51 int __libc_multiple_libcs = 0; /* Defining this here avoids the inclusion
52 of init-first. */
53 /* This variable containts the lowest stack address ever used. */
54 void *__libc_stack_end;
55
56 #if HP_TIMING_AVAIL
57 hp_timing_t _dl_cpuclock_offset;
58 #endif
59
60
61 struct hurd_startup_data *_dl_hurd_data;
62
63 /* This is used only within ld.so, via dl-minimal.c's __errno_location. */
64 #undef errno
65 int errno attribute_hidden;
66
67 /* Defining these variables here avoids the inclusion of hurdsig.c. */
68 unsigned long int __hurd_sigthread_stack_base;
69 unsigned long int __hurd_sigthread_stack_end;
70 unsigned long int *__hurd_sigthread_variables;
71
72 /* Defining these variables here avoids the inclusion of init-first.c.
73 We need to provide temporary storage for the per-thread variables
74 of the main user thread here, since it is used for storing the
75 `errno' variable. Note that this information is lost once we
76 relocate the dynamic linker. */
77 static unsigned long int threadvars[_HURD_THREADVAR_MAX];
78 unsigned long int __hurd_threadvar_stack_offset
79 = (unsigned long int) &threadvars;
80 unsigned long int __hurd_threadvar_stack_mask;
81
82 #define FMH defined(__i386__)
83 #if ! FMH
84 # define fmh() ((void)0)
85 # define unfmh() ((void)0)
86 #else
87 /* XXX loser kludge for vm_map kernel bug */
88 #undef ELF_MACHINE_USER_ADDRESS_MASK
89 #define ELF_MACHINE_USER_ADDRESS_MASK 0
90 static vm_address_t fmha;
91 static vm_size_t fmhs;
92 static void unfmh(void){
93 __vm_deallocate(__mach_task_self(),fmha,fmhs);}
94 static void fmh(void) {
95 error_t err;int x;mach_port_t p;
96 vm_address_t a=0x08000000U,max=VM_MAX_ADDRESS;
97 while (!(err=__vm_region(__mach_task_self(),&a,&fmhs,&x,&x,&x,&x,&p,&x))){
98 __mach_port_deallocate(__mach_task_self(),p);
99 if (a+fmhs>=0x80000000U){
100 max=a; break;}
101 fmha=a+=fmhs;}
102 if (err) assert(err==KERN_NO_SPACE);
103 if (!fmha)fmhs=0;else{
104 fmhs=max-fmha;
105 err = __vm_map (__mach_task_self (),
106 &fmha, fmhs, 0, 0, MACH_PORT_NULL, 0, 1,
107 VM_PROT_NONE, VM_PROT_NONE, VM_INHERIT_COPY);
108 assert_perror(err);}
109 }
110 /* XXX loser kludge for vm_map kernel bug */
111 #endif
112
113
114 ElfW(Addr)
115 _dl_sysdep_start (void **start_argptr,
116 void (*dl_main) (const ElfW(Phdr) *phdr, ElfW(Word) phent,
117 ElfW(Addr) *user_entry,
118 ElfW(auxv_t) *auxv))
119 {
120 void go (intptr_t *argdata)
121 {
122 extern unsigned int _dl_skip_args; /* rtld.c */
123 char **p;
124
125 /* Cache the information in various global variables. */
126 _dl_argc = *argdata;
127 _dl_argv = 1 + (char **) argdata;
128 _environ = &_dl_argv[_dl_argc + 1];
129 for (p = _environ; *p++;); /* Skip environ pointers and terminator. */
130
131 if ((void *) p == _dl_argv[0])
132 {
133 static struct hurd_startup_data nodata;
134 _dl_hurd_data = &nodata;
135 nodata.user_entry = (vm_address_t) ENTRY_POINT;
136 }
137 else
138 _dl_hurd_data = (void *) p;
139
140 INTUSE(__libc_enable_secure) = _dl_hurd_data->flags & EXEC_SECURE;
141
142 if (_dl_hurd_data->flags & EXEC_STACK_ARGS &&
143 _dl_hurd_data->user_entry == 0)
144 _dl_hurd_data->user_entry = (vm_address_t) ENTRY_POINT;
145
146 unfmh(); /* XXX */
147
148 #if 0 /* XXX make this work for real someday... */
149 if (_dl_hurd_data->user_entry == (vm_address_t) ENTRY_POINT)
150 /* We were invoked as a command, not as the program interpreter.
151 The generic ld.so code supports this: it will parse the args
152 as "ld.so PROGRAM [ARGS...]". For booting the Hurd, we
153 support an additional special syntax:
154 ld.so [-LIBS...] PROGRAM [ARGS...]
155 Each LIBS word consists of "FILENAME=MEMOBJ";
156 for example "-/lib/libc.so=123" says that the contents of
157 /lib/libc.so are found in a memory object whose port name
158 in our task is 123. */
159 while (_dl_argc > 2 && _dl_argv[1][0] == '-' && _dl_argv[1][1] != '-')
160 {
161 char *lastslash, *memobjname, *p;
162 struct link_map *l;
163 mach_port_t memobj;
164 error_t err;
165
166 ++_dl_skip_args;
167 --_dl_argc;
168 p = _dl_argv++[1] + 1;
169
170 memobjname = strchr (p, '=');
171 if (! memobjname)
172 _dl_sysdep_fatal ("Bogus library spec: ", p, "\n", NULL);
173 *memobjname++ = '\0';
174 memobj = 0;
175 while (*memobjname != '\0')
176 memobj = (memobj * 10) + (*memobjname++ - '0');
177
178 /* Add a user reference on the memory object port, so we will
179 still have one after _dl_map_object_from_fd calls our
180 `close'. */
181 err = __mach_port_mod_refs (__mach_task_self (), memobj,
182 MACH_PORT_RIGHT_SEND, +1);
183 assert_perror (err);
184
185 lastslash = strrchr (p, '/');
186 l = _dl_map_object_from_fd (lastslash ? lastslash + 1 : p,
187 memobj, strdup (p), 0);
188
189 /* Squirrel away the memory object port where it
190 can be retrieved by the program later. */
191 l->l_info[DT_NULL] = (void *) memobj;
192 }
193 #endif
194
195 /* Call elf/rtld.c's main program. It will set everything
196 up and leave us to transfer control to USER_ENTRY. */
197 (*dl_main) ((const ElfW(Phdr) *) _dl_hurd_data->phdr,
198 _dl_hurd_data->phdrsz / sizeof (ElfW(Phdr)),
199 &_dl_hurd_data->user_entry, NULL);
200
201 /* The call above might screw a few things up.
202
203 First of all, if _dl_skip_args is nonzero, we are ignoring
204 the first few arguments. However, if we have no Hurd startup
205 data, it is the magical convention that ARGV[0] == P. The
206 startup code in init-first.c will get confused if this is not
207 the case, so we must rearrange things to make it so. We'll
208 overwrite the origional ARGV[0] at P with ARGV[_dl_skip_args].
209
210 Secondly, if we need to be secure, it removes some dangerous
211 environment variables. If we have no Hurd startup date this
212 changes P (since that's the location after the terminating
213 NULL in the list of environment variables). We do the same
214 thing as in the first case but make sure we recalculate P.
215 If we do have Hurd startup data, we have to move the data
216 such that it starts just after the terminating NULL in the
217 environment list.
218
219 We use memmove, since the locations might overlap. */
220 if (INTUSE(__libc_enable_secure) || _dl_skip_args)
221 {
222 char **newp;
223
224 for (newp = _environ; *newp++;);
225
226 if (_dl_argv[-_dl_skip_args] == (char *) p)
227 {
228 if ((char *) newp != _dl_argv[0])
229 {
230 assert ((char *) newp < _dl_argv[0]);
231 _dl_argv[0] = memmove ((char *) newp, _dl_argv[0],
232 strlen (_dl_argv[0]) + 1);
233 }
234 }
235 else
236 {
237 if ((void *) newp != _dl_hurd_data)
238 memmove (newp, _dl_hurd_data, sizeof (*_dl_hurd_data));
239 }
240 }
241
242 {
243 extern void _dl_start_user (void);
244 /* Unwind the stack to ARGDATA and simulate a return from _dl_start
245 to the RTLD_START code which will run the user's entry point. */
246 RETURN_TO (argdata, &_dl_start_user, _dl_hurd_data->user_entry);
247 }
248 }
249
250 /* Set up so we can do RPCs. */
251 __mach_init ();
252
253 /* Initialize frequently used global variable. */
254 GLRO(dl_pagesize) = __getpagesize ();
255
256 #if HP_TIMING_AVAIL
257 HP_TIMING_NOW (_dl_cpuclock_offset);
258 #endif
259
260 fmh(); /* XXX */
261
262 /* See hurd/hurdstartup.c; this deals with getting information
263 from the exec server and slicing up the arguments.
264 Then it will call `go', above. */
265 _hurd_startup (start_argptr, &go);
266
267 LOSE;
268 abort ();
269 }
270
271 void
272 internal_function
273 _dl_sysdep_start_cleanup (void)
274 {
275 /* Deallocate the reply port and task port rights acquired by
276 __mach_init. We are done with them now, and the user will
277 reacquire them for himself when he wants them. */
278 __mig_dealloc_reply_port (MACH_PORT_NULL);
279 __mach_port_deallocate (__mach_task_self (), __mach_task_self_);
280 }
281 \f
282 /* Minimal open/close/mmap implementation sufficient for initial loading of
283 shared libraries. These are weak definitions so that when the
284 dynamic linker re-relocates itself to be user-visible (for -ldl),
285 it will get the user's definition (i.e. usually libc's). */
286
287 /* Open FILE_NAME and return a Hurd I/O for it in *PORT, or return an
288 error. If STAT is non-zero, stat the file into that stat buffer. */
289 static error_t
290 open_file (const char *file_name, int flags,
291 mach_port_t *port, struct stat64 *stat)
292 {
293 enum retry_type doretry;
294 char retryname[1024]; /* XXX string_t LOSES! */
295 file_t startdir;
296 error_t err;
297
298 error_t use_init_port (int which, error_t (*operate) (file_t))
299 {
300 return (which < _dl_hurd_data->portarraysize
301 ? ((*operate) (_dl_hurd_data->portarray[which]))
302 : EGRATUITOUS);
303 }
304 file_t get_dtable_port (int fd)
305 {
306 if ((unsigned int) fd < _dl_hurd_data->dtablesize
307 && _dl_hurd_data->dtable[fd] != MACH_PORT_NULL)
308 {
309 __mach_port_mod_refs (__mach_task_self (), _dl_hurd_data->dtable[fd],
310 MACH_PORT_RIGHT_SEND, +1);
311 return _dl_hurd_data->dtable[fd];
312 }
313 errno = EBADF;
314 return MACH_PORT_NULL;
315 }
316
317 assert (!(flags & ~(O_READ | O_CLOEXEC)));
318
319 startdir = _dl_hurd_data->portarray[file_name[0] == '/' ?
320 INIT_PORT_CRDIR : INIT_PORT_CWDIR];
321
322 while (file_name[0] == '/')
323 file_name++;
324
325 err = __dir_lookup (startdir, (char *)file_name, O_RDONLY, 0,
326 &doretry, retryname, port);
327
328 if (!err)
329 err = __hurd_file_name_lookup_retry (use_init_port, get_dtable_port,
330 __dir_lookup, doretry, retryname,
331 O_RDONLY, 0, port);
332 if (!err && stat)
333 {
334 err = __io_stat (*port, stat);
335 if (err)
336 __mach_port_deallocate (__mach_task_self (), *port);
337 }
338
339 return err;
340 }
341
342 int weak_function
343 __open (const char *file_name, int mode, ...)
344 {
345 mach_port_t port;
346 error_t err = open_file (file_name, mode, &port, 0);
347 if (err)
348 return __hurd_fail (err);
349 else
350 return (int)port;
351 }
352
353 int weak_function
354 __close (int fd)
355 {
356 if (fd != (int) MACH_PORT_NULL)
357 __mach_port_deallocate (__mach_task_self (), (mach_port_t) fd);
358 return 0;
359 }
360
361 __ssize_t weak_function
362 __libc_read (int fd, void *buf, size_t nbytes)
363 {
364 error_t err;
365 char *data;
366 mach_msg_type_number_t nread;
367
368 data = buf;
369 nread = nbytes;
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 if (fd >= _hurd_init_dtablesize)
405 {
406 errno = EBADF;
407 return -1;
408 }
409
410 int i;
411 size_t total = 0;
412 for (i = 0; i < niov; ++i)
413 total += iov[i].iov_len;
414
415 if (total != 0)
416 {
417 char buf[total], *bufp = buf;
418 error_t err;
419 mach_msg_type_number_t nwrote;
420
421 for (i = 0; i < niov; ++i)
422 bufp = (memcpy (bufp, iov[i].iov_base, iov[i].iov_len)
423 + iov[i].iov_len);
424
425 err = __io_write (_hurd_init_dtable[fd], buf, total, -1, &nwrote);
426 if (err)
427 return __hurd_fail (err);
428
429 return nwrote;
430 }
431 return 0;
432 }
433
434
435 off64_t weak_function
436 __libc_lseek64 (int fd, off64_t offset, int whence)
437 {
438 error_t err;
439
440 err = __io_seek ((mach_port_t) fd, offset, whence, &offset);
441 if (err)
442 return __hurd_fail (err);
443
444 return offset;
445 }
446
447 __ptr_t weak_function
448 __mmap (__ptr_t addr, size_t len, int prot, int flags, int fd, off_t offset)
449 {
450 error_t err;
451 vm_prot_t vmprot;
452 vm_address_t mapaddr;
453 mach_port_t memobj_rd, memobj_wr;
454
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;
462
463 if (flags & MAP_ANON)
464 memobj_rd = MACH_PORT_NULL;
465 else
466 {
467 assert (!(flags & MAP_SHARED));
468 err = __io_map ((mach_port_t) fd, &memobj_rd, &memobj_wr);
469 if (err)
470 return __hurd_fail (err), MAP_FAILED;
471 __mach_port_deallocate (__mach_task_self (), memobj_wr);
472 }
473
474 mapaddr = (vm_address_t) addr;
475 err = __vm_map (__mach_task_self (),
476 &mapaddr, (vm_size_t) len, ELF_MACHINE_USER_ADDRESS_MASK,
477 !(flags & MAP_FIXED),
478 memobj_rd,
479 (vm_offset_t) offset,
480 flags & (MAP_COPY|MAP_PRIVATE),
481 vmprot, VM_PROT_ALL,
482 (flags & MAP_SHARED) ? VM_INHERIT_SHARE : VM_INHERIT_COPY);
483 if (err == KERN_NO_SPACE && (flags & MAP_FIXED))
484 {
485 /* XXX this is not atomic as it is in unix! */
486 /* The region is already allocated; deallocate it first. */
487 err = __vm_deallocate (__mach_task_self (), mapaddr, len);
488 if (! err)
489 err = __vm_map (__mach_task_self (),
490 &mapaddr, (vm_size_t) len,
491 ELF_MACHINE_USER_ADDRESS_MASK,
492 !(flags & MAP_FIXED),
493 memobj_rd, (vm_offset_t) offset,
494 flags & (MAP_COPY|MAP_PRIVATE),
495 vmprot, VM_PROT_ALL,
496 (flags & MAP_SHARED)
497 ? VM_INHERIT_SHARE : VM_INHERIT_COPY);
498 }
499
500 if ((flags & MAP_ANON) == 0)
501 __mach_port_deallocate (__mach_task_self (), memobj_rd);
502
503 if (err)
504 return __hurd_fail (err), MAP_FAILED;
505 return (__ptr_t) mapaddr;
506 }
507
508 int weak_function
509 __fxstat64 (int vers, int fd, struct stat64 *buf)
510 {
511 error_t err;
512
513 assert (vers == _STAT_VER);
514
515 err = __io_stat ((mach_port_t) fd, buf);
516 if (err)
517 return __hurd_fail (err);
518
519 return 0;
520 }
521 libc_hidden_def (__fxstat64)
522
523 int weak_function
524 __xstat64 (int vers, const char *file, struct stat64 *buf)
525 {
526 error_t err;
527 mach_port_t port;
528
529 assert (vers == _STAT_VER);
530
531 err = open_file (file, 0, &port, buf);
532 if (err)
533 return __hurd_fail (err);
534
535 __mach_port_deallocate (__mach_task_self (), port);
536
537 return 0;
538 }
539 libc_hidden_def (__xstat64)
540
541 /* This function is called by the dynamic linker (rtld.c) to check
542 whether debugging malloc is allowed even for SUID binaries. This
543 stub will always fail, which means that malloc-debugging is always
544 disabled for SUID binaries. */
545 int weak_function
546 __access (const char *file, int type)
547 {
548 errno = ENOSYS;
549 return -1;
550 }
551
552 pid_t weak_function
553 __getpid ()
554 {
555 pid_t pid, ppid;
556 int orphaned;
557
558 if (__proc_getpids (_dl_hurd_data->portarray[INIT_PORT_PROC],
559 &pid, &ppid, &orphaned))
560 return -1;
561
562 return pid;
563 }
564
565 /* This is called only in some strange cases trying to guess a value
566 for $ORIGIN for the executable. The dynamic linker copes with
567 getcwd failing (dl-object.c), and it's too much hassle to include
568 the functionality here. (We could, it just requires duplicating or
569 reusing getcwd.c's code but using our special lookup function as in
570 `open', above.) */
571 char *
572 weak_function
573 __getcwd (char *buf, size_t size)
574 {
575 errno = ENOSYS;
576 return NULL;
577 }
578
579 void weak_function attribute_hidden
580 _exit (int status)
581 {
582 __proc_mark_exit (_dl_hurd_data->portarray[INIT_PORT_PROC],
583 W_EXITCODE (status, 0), 0);
584 while (__task_terminate (__mach_task_self ()))
585 __mach_task_self_ = (__mach_task_self) ();
586 }
587 /* We need this alias to satisfy references from libc_pic.a objects
588 that were affected by the libc_hidden_proto declaration for _exit. */
589 strong_alias (_exit, __GI__exit)
590
591 /* Try to get a machine dependent instruction which will make the
592 program crash. This is used in case everything else fails. */
593 #include <abort-instr.h>
594 #ifndef ABORT_INSTRUCTION
595 /* No such instruction is available. */
596 # define ABORT_INSTRUCTION
597 #endif
598
599 void weak_function
600 abort (void)
601 {
602 /* Try to abort using the system specific command. */
603 ABORT_INSTRUCTION;
604
605 /* If the abort instruction failed, exit. */
606 _exit (127);
607
608 /* If even this fails, make sure we never return. */
609 while (1)
610 /* Try for ever and ever. */
611 ABORT_INSTRUCTION;
612 }
613
614 /* We need this alias to satisfy references from libc_pic.a objects
615 that were affected by the libc_hidden_proto declaration for abort. */
616 strong_alias (abort, __GI_abort)
617 \f
618 /* This function is called by interruptible RPC stubs. For initial
619 dynamic linking, just use the normal mach_msg. Since this defn is
620 weak, the real defn in libc.so will override it if we are linked into
621 the user program (-ldl). */
622
623 error_t weak_function
624 _hurd_intr_rpc_mach_msg (mach_msg_header_t *msg,
625 mach_msg_option_t option,
626 mach_msg_size_t send_size,
627 mach_msg_size_t rcv_size,
628 mach_port_t rcv_name,
629 mach_msg_timeout_t timeout,
630 mach_port_t notify)
631 {
632 return __mach_msg (msg, option, send_size, rcv_size, rcv_name,
633 timeout, notify);
634 }
635
636
637 void
638 internal_function
639 _dl_show_auxv (void)
640 {
641 /* There is nothing to print. Hurd has no auxiliary vector. */
642 }
643
644
645 /* Return an array of useful/necessary hardware capability names. */
646 const struct r_strlenpair *
647 internal_function
648 _dl_important_hwcaps (const char *platform, size_t platform_len, size_t *sz,
649 size_t *max_capstrlen)
650 {
651 struct r_strlenpair *result;
652
653 /* Return an empty array. Hurd has no hardware capabilities. */
654 result = (struct r_strlenpair *) malloc (sizeof (*result));
655 if (result == NULL)
656 _dl_signal_error (ENOMEM, NULL, NULL, "cannot create capability list");
657
658 result[0].str = (char *) result; /* Does not really matter. */
659 result[0].len = 0;
660
661 *sz = 1;
662 return result;
663 }
664
665 void weak_function
666 _dl_init_first (int argc, ...)
667 {
668 /* This no-op definition only gets used if libc is not linked in. */
669 }