]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mach/hurd/dl-sysdep.c
Wed Nov 15 19:22:07 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[thirdparty/glibc.git] / sysdeps / mach / hurd / dl-sysdep.c
1 /* Operating system support for run-time dynamic linker. Hurd version.
2 Copyright (C) 1995 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
19
20 #include <hurd.h>
21 #include <link.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <stdlib.h>
25 #include <sys/mman.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 <mach/host_info.h>
32 #include "../stdio-common/_itoa.h"
33 #include <hurd/auth.h>
34 #include <hurd/term.h>
35 #include <stdarg.h>
36
37 #include "dl-machine.h"
38
39 extern void __mach_init (void);
40
41 extern int _dl_argc;
42 extern char **_dl_argv;
43 extern char **_environ;
44
45 struct hurd_startup_data *_dl_hurd_data;
46
47 unsigned int __hurd_threadvar_max = _HURD_THREADVAR_MAX;
48 static unsigned long int threadvars[_HURD_THREADVAR_MAX];
49 unsigned long int __hurd_threadvar_stack_offset
50 = (unsigned long int) &threadvars;
51
52
53 /* XXX loser kludge for vm_map kernel bug */
54 static vm_address_t fmha;
55 static vm_size_t fmhs;
56 static void unfmh(){
57 __vm_deallocate(__mach_task_self(),fmha,fmhs);}
58 static void fmh() {
59 error_t err;int x;mach_port_t p;
60 vm_address_t a=0x08000000U,max=VM_MAX_ADDRESS;
61 while (!(err=__vm_region(__mach_task_self(),&a,&fmhs,&x,&x,&x,&x,&p,&x))){
62 __mach_port_deallocate(__mach_task_self(),p);
63 if (a+fmhs>=0x80000000U){
64 max=a; break;}
65 fmha=a+=fmhs;}
66 if (err) assert(err==KERN_NO_SPACE);
67 if (!fmha)fmhs=0;else{
68 fmhs=max-fmha;
69 err = __vm_map (__mach_task_self (),
70 &fmha, fmhs, 0, 0, MACH_PORT_NULL, 0, 1,
71 VM_PROT_NONE, VM_PROT_NONE, VM_INHERIT_COPY);
72 assert_perror(err);}
73 }
74 /* XXX loser kludge for vm_map kernel bug */
75
76
77
78 Elf32_Addr
79 _dl_sysdep_start (void **start_argptr,
80 void (*dl_main) (const Elf32_Phdr *phdr, Elf32_Word phent,
81 Elf32_Addr *user_entry))
82 {
83 extern void _start ();
84
85 void go (int *argdata)
86 {
87 extern unsigned int _dl_skip_args; /* rtld.c */
88 char **p;
89
90 /* Cache the information in various global variables. */
91 _dl_argc = *argdata;
92 _dl_argv = 1 + (char **) argdata;
93 _environ = &_dl_argv[_dl_argc + 1];
94 for (p = _environ; *p++;); /* Skip environ pointers and terminator. */
95
96 if ((void *) p == _dl_argv[0])
97 {
98 static struct hurd_startup_data nodata;
99 _dl_hurd_data = &nodata;
100 nodata.user_entry = (vm_address_t) &_start;
101 }
102 else
103 _dl_hurd_data = (void *) p;
104
105 _dl_secure = _dl_hurd_data->flags & EXEC_SECURE;
106
107 if (_dl_hurd_data->flags & EXEC_STACK_ARGS &&
108 _dl_hurd_data->user_entry == 0)
109 _dl_hurd_data->user_entry = (vm_address_t) &_start;
110
111 unfmh(); /* XXX */
112
113 if (_dl_hurd_data->user_entry == (vm_address_t) &_start)
114 /* We were invoked as a command, not as the program interpreter.
115 The generic ld.so code supports this: it will parse the args
116 as "ld.so PROGRAM [ARGS...]". For booting the Hurd, we
117 support an additional special syntax:
118 ld.so [-LIBS...] PROGRAM [ARGS...]
119 Each LIBS word consists of "FILENAME=MEMOBJ";
120 for example "-/lib/libc.so=123" says that the contents of
121 /lib/libc.so are found in a memory object whose port name
122 in our task is 123. */
123 while (_dl_argc > 2 && _dl_argv[1][0] == '-' && _dl_argv[1][1] != '-')
124 {
125 char *lastslash, *memobjname, *p;
126 struct link_map *l;
127 mach_port_t memobj;
128 error_t err;
129
130 ++_dl_skip_args;
131 --_dl_argc;
132 p = _dl_argv++[1] + 1;
133
134 memobjname = strchr (p, '=');
135 if (! memobjname)
136 _dl_sysdep_fatal ("Bogus library spec: ", p, "\n", NULL);
137 *memobjname++ = '\0';
138 memobj = (mach_port_t) atoi (memobjname);
139
140 /* Add a user reference on the memory object port, so we will
141 still have one after _dl_map_object_from_fd calls our
142 `close'. */
143 err = __mach_port_mod_refs (__mach_task_self (), memobj,
144 MACH_PORT_RIGHT_SEND, +1);
145 assert_perror (err);
146
147 lastslash = strrchr (p, '/');
148 l = _dl_map_object_from_fd (lastslash ? lastslash + 1 : p,
149 memobj, strdup (p));
150
151 /* Squirrel away the memory object port where it
152 can be retrieved by the program later. */
153 l->l_info[DT_NULL] = (void *) memobj;
154 }
155
156 /* Call elf/rtld.c's main program. It will set everything
157 up and leave us to transfer control to USER_ENTRY. */
158 (*dl_main) ((const Elf32_Phdr *) _dl_hurd_data->phdr,
159 _dl_hurd_data->phdrsz / sizeof (Elf32_Phdr),
160 &_dl_hurd_data->user_entry);
161
162 /* Deallocate the reply port and task port rights acquired by
163 __mach_init. We are done with them now, and the user will
164 reacquire them for himself when he wants them. */
165 __mig_dealloc_reply_port (MACH_PORT_NULL);
166 __mach_port_deallocate (__mach_task_self (), __mach_task_self_);
167
168 if (_dl_skip_args && _dl_argv[-_dl_skip_args] == (char *) p)
169 {
170 /* We are ignoring the first few arguments, but we have no Hurd
171 startup data. It is magical convention that ARGV[0] == P in
172 this case. The startup code in init-first.c will get confused
173 if this is not the case, so we must rearrange things to make
174 it so. Overwrite the original ARGV[0] at P with
175 ARGV[_dl_skip_args]. */
176 assert ((char *) p < _dl_argv[0]);
177 _dl_argv[0] = strcpy ((char *) p, _dl_argv[0]);
178 }
179
180 {
181 extern void _dl_start_user (void);
182 /* Unwind the stack to ARGDATA and simulate a return from _dl_start
183 to the RTLD_START code which will run the user's entry point. */
184 RETURN_TO (argdata, &_dl_start_user, _dl_hurd_data->user_entry);
185 }
186 }
187
188 /* Set up so we can do RPCs. */
189 __mach_init ();
190
191 fmh(); /* XXX */
192
193 /* See hurd/hurdstartup.c; this deals with getting information
194 from the exec server and slicing up the arguments.
195 Then it will call `go', above. */
196 _hurd_startup (start_argptr, &go);
197
198 LOSE;
199 abort ();
200 }
201 \f
202 int
203 _dl_sysdep_open_zero_fill (void)
204 {
205 return (int) MACH_PORT_NULL;
206 }
207
208
209 void
210 _dl_sysdep_fatal (const char *msg, ...)
211 {
212 va_list ap;
213
214 va_start (ap, msg);
215 do
216 {
217 size_t len = strlen (msg);
218 mach_msg_type_number_t nwrote;
219 do
220 {
221 if (__io_write (_hurd_init_dtable[2], msg, len, -1, &nwrote))
222 break;
223 len -= nwrote;
224 msg += nwrote;
225 } while (nwrote > 0);
226 msg = va_arg (ap, const char *);
227 } while (msg);
228 va_end (ap);
229
230 _exit (127);
231 }
232
233
234 void
235 _dl_sysdep_message (const char *msg, ...)
236 {
237 va_list ap;
238
239 va_start (ap, msg);
240 do
241 {
242 size_t len = strlen (msg);
243 mach_msg_type_number_t nwrote;
244 do
245 {
246 if (__io_write (_hurd_init_dtable[1], msg, len, -1, &nwrote))
247 break;
248 len -= nwrote;
249 msg += nwrote;
250 } while (nwrote > 0);
251 msg = va_arg (ap, const char *);
252 } while (msg);
253 va_end (ap);
254 }
255 \f
256 /* Minimal open/close/mmap implementation sufficient for initial loading of
257 shared libraries. These are weak definitions so that when the
258 dynamic linker re-relocates itself to be user-visible (for -ldl),
259 it will get the user's definition (i.e. usually libc's). */
260
261 int
262 open (const char *file_name, int mode, ...)
263 {
264 enum retry_type doretry;
265 char retryname[1024]; /* XXX string_t LOSES! */
266 file_t startdir, newpt, fileport;
267 int dealloc_dir;
268 int nloops;
269 error_t err;
270
271 assert (mode == O_RDONLY);
272
273 startdir = _dl_hurd_data->portarray[file_name[0] == '/' ?
274 INIT_PORT_CRDIR : INIT_PORT_CWDIR];
275
276 while (file_name[0] == '/')
277 file_name++;
278
279 if (err = __dir_lookup (startdir, file_name, mode, 0,
280 &doretry, retryname, &fileport))
281 return __hurd_fail (err);
282
283 dealloc_dir = 0;
284 nloops = 0;
285
286 while (1)
287 {
288 if (dealloc_dir)
289 __mach_port_deallocate (__mach_task_self (), startdir);
290 if (err)
291 return __hurd_fail (err);
292
293 switch (doretry)
294 {
295 case FS_RETRY_REAUTH:
296 {
297 mach_port_t ref = __mach_reply_port ();
298 err = __io_reauthenticate (fileport, ref, MACH_MSG_TYPE_MAKE_SEND);
299 if (! err)
300 err = __auth_user_authenticate
301 (_dl_hurd_data->portarray[INIT_PORT_AUTH],
302 fileport,
303 ref, MACH_MSG_TYPE_MAKE_SEND,
304 &newpt);
305 __mach_port_destroy (__mach_task_self (), ref);
306 }
307 __mach_port_deallocate (__mach_task_self (), fileport);
308 if (err)
309 return __hurd_fail (err);
310 fileport = newpt;
311 /* Fall through. */
312
313 case FS_RETRY_NORMAL:
314 #ifdef SYMLOOP_MAX
315 if (nloops++ >= SYMLOOP_MAX)
316 return __hurd_fail (ELOOP);
317 #endif
318
319 /* An empty RETRYNAME indicates we have the final port. */
320 if (retryname[0] == '\0')
321 {
322 mach_port_t memobj_rd, memobj_wr;
323
324 dealloc_dir = 1;
325
326 opened:
327 /* We have the file open. Now map it. */
328 err = __io_map (fileport, &memobj_rd, &memobj_wr);
329 if (dealloc_dir)
330 __mach_port_deallocate (__mach_task_self (), fileport);
331 if (err)
332 return __hurd_fail (err);
333 if (memobj_wr != MACH_PORT_NULL)
334 __mach_port_deallocate (__mach_task_self (), memobj_wr);
335
336 return (int) memobj_rd;
337 }
338
339 startdir = fileport;
340 dealloc_dir = 1;
341 file_name = retryname;
342 break;
343
344 case FS_RETRY_MAGICAL:
345 switch (retryname[0])
346 {
347 case '/':
348 startdir = _dl_hurd_data->portarray[INIT_PORT_CRDIR];
349 dealloc_dir = 0;
350 if (fileport != MACH_PORT_NULL)
351 __mach_port_deallocate (__mach_task_self (), fileport);
352 file_name = &retryname[1];
353 break;
354
355 case 'f':
356 if (retryname[1] == 'd' && retryname[2] == '/')
357 {
358 int fd;
359 char *end;
360 err = 0;
361 fd = (int) strtol (retryname, &end, 10);
362 if (end == NULL || err || /* Malformed number. */
363 /* Check for excess text after the number. A slash
364 is valid; it ends the component. Anything else
365 does not name a numeric file descriptor. */
366 (*end != '/' && *end != '\0'))
367 return __hurd_fail (ENOENT);
368 if (fd < 0 || fd >= _dl_hurd_data->dtablesize ||
369 _dl_hurd_data->dtable[fd] == MACH_PORT_NULL)
370 /* If the name was a proper number, but the file
371 descriptor does not exist, we return EBADF instead
372 of ENOENT. */
373 return __hurd_fail (EBADF);
374 fileport = _dl_hurd_data->dtable[fd];
375 if (*end == '\0')
376 {
377 /* This descriptor is the file port we want. */
378 dealloc_dir = 0;
379 goto opened;
380 }
381 else
382 {
383 /* Do a normal retry on the remaining components. */
384 startdir = fileport;
385 dealloc_dir = 1;
386 file_name = end + 1; /* Skip the slash. */
387 break;
388 }
389 }
390 else
391 goto bad_magic;
392 break;
393
394 case 'm':
395 if (retryname[1] == 'a' && retryname[2] == 'c' &&
396 retryname[3] == 'h' && retryname[4] == 't' &&
397 retryname[5] == 'y' && retryname[6] == 'p' &&
398 retryname[7] == 'e')
399 {
400 error_t err;
401 struct host_basic_info hostinfo;
402 mach_msg_type_number_t hostinfocnt = HOST_BASIC_INFO_COUNT;
403 char *p;
404 if (err = __host_info (__mach_host_self (), HOST_BASIC_INFO,
405 (natural_t *) &hostinfo,
406 &hostinfocnt))
407 return err;
408 if (hostinfocnt != HOST_BASIC_INFO_COUNT)
409 return EGRATUITOUS;
410 p = _itoa (hostinfo.cpu_subtype, &retryname[8], 10, 0);
411 *--p = '/';
412 p = _itoa (hostinfo.cpu_type, &retryname[8], 10, 0);
413 if (p < retryname)
414 abort (); /* XXX write this right if this ever happens */
415 if (p > retryname)
416 strcpy (retryname, p);
417 startdir = fileport;
418 dealloc_dir = 1;
419 }
420 else
421 goto bad_magic;
422 break;
423
424 case 't':
425 if (retryname[1] == 't' && retryname[2] == 'y')
426 switch (retryname[3])
427 {
428 error_t opentty (file_t *result)
429 {
430 error_t err;
431 file_t unauth;
432 err = __termctty_open_terminal
433 (_dl_hurd_data->portarray[INIT_PORT_CTTYID],
434 mode, &unauth);
435 if (! err)
436 {
437 mach_port_t ref = __mach_reply_port ();
438 err = __io_reauthenticate
439 (unauth, ref, MACH_MSG_TYPE_MAKE_SEND);
440 if (! err)
441 err = __auth_user_authenticate
442 (_dl_hurd_data->portarray[INIT_PORT_AUTH],
443 unauth,
444 ref, MACH_MSG_TYPE_MAKE_SEND,
445 result);
446 __mach_port_deallocate (__mach_task_self (),
447 unauth);
448 __mach_port_destroy (__mach_task_self (), ref);
449 }
450 return err;
451 }
452
453 case '\0':
454 if (err = opentty (&fileport))
455 return __hurd_fail (err);
456 dealloc_dir = 1;
457 goto opened;
458 case '/':
459 if (err = opentty (&startdir))
460 return __hurd_fail (err);
461 dealloc_dir = 1;
462 strcpy (retryname, &retryname[4]);
463 break;
464 default:
465 goto bad_magic;
466 }
467 else
468 goto bad_magic;
469 break;
470
471 default:
472 bad_magic:
473 return __hurd_fail (EGRATUITOUS);
474 }
475 break;
476
477 default:
478 return __hurd_fail (EGRATUITOUS);
479 }
480
481 err = __dir_lookup (startdir, file_name, mode, 0,
482 &doretry, retryname, &fileport);
483 }
484 }
485
486 int
487 close (int fd)
488 {
489 if (fd != (int) MACH_PORT_NULL)
490 __mach_port_deallocate (__mach_task_self (), (mach_port_t) fd);
491 return 0;
492 }
493
494 caddr_t
495 mmap (caddr_t addr, size_t len, int prot, int flags, int fd, off_t offset)
496 {
497 error_t err;
498 vm_prot_t vmprot;
499 vm_address_t mapaddr;
500
501 vmprot = VM_PROT_NONE;
502 if (prot & PROT_READ)
503 vmprot |= VM_PROT_READ;
504 if (prot & PROT_WRITE)
505 vmprot |= VM_PROT_WRITE;
506 if (prot & PROT_EXEC)
507 vmprot |= VM_PROT_EXECUTE;
508
509 mapaddr = (vm_address_t) addr;
510 err = __vm_map (__mach_task_self (),
511 &mapaddr, (vm_size_t) len, 0 /*ELF_MACHINE_USER_ADDRESS_MASK*/,
512 !(flags & MAP_FIXED),
513 (mach_port_t) fd, (vm_offset_t) offset,
514 flags & (MAP_COPY|MAP_PRIVATE),
515 vmprot, VM_PROT_ALL,
516 (flags & MAP_INHERIT) ? VM_INHERIT_COPY : VM_INHERIT_NONE);
517 return err ? (caddr_t) __hurd_fail (err) : (caddr_t) mapaddr;
518 }
519
520 void
521 _exit (int status)
522 {
523 __proc_mark_exit (_dl_hurd_data->portarray[INIT_PORT_PROC],
524 W_EXITCODE (status, 0));
525 while (__task_terminate (__mach_task_self ()))
526 __mach_task_self_ = (__mach_task_self) ();
527 }
528
529 weak_symbol (_exit)
530 weak_symbol (open)
531 weak_symbol (close)
532 weak_symbol (mmap)
533 \f
534 /* Minimal `malloc' allocator for use while loading shared libraries.
535 Only small blocks are allocated, and none are ever freed. */
536
537 void *
538 malloc (size_t n)
539 {
540 static vm_address_t ptr, end;
541 void *block;
542
543 if (end == 0)
544 {
545 /* Consume any unused space in the last page of our data segment. */
546 extern char _end;
547 ptr = (vm_address_t) &_end;
548 end = round_page (ptr);
549 }
550
551 /* Make sure the allocation pointer is ideally aligned. */
552 ptr += sizeof (double) - 1;
553 ptr &= ~(sizeof (double) - 1);
554
555 if (ptr + n >= end)
556 {
557 /* Insufficient space left; allocate another page. */
558 vm_address_t page;
559 assert (n <= __vm_page_size);
560 __vm_allocate (__mach_task_self (), &page, __vm_page_size, 1);
561 if (page != end)
562 ptr = page;
563 end = page + __vm_page_size;
564 }
565
566 block = (void *) ptr;
567 ptr += n;
568 return block;
569 }
570
571 weak_symbol (malloc)
572
573 /* These should never be called. */
574 void *realloc (void *ptr, size_t n) { ptr += n; abort (); }
575 void free (void *ptr) { ptr = ptr; abort (); }
576 weak_symbol (realloc)
577 weak_symbol (free)
578 \f
579 /* Avoid signal frobnication in setjmp/longjmp. */
580
581 int __sigjmp_save (sigjmp_buf env, int savemask)
582 { env[0].__mask_was_saved = savemask; return 0; }
583 weak_symbol (__sigjmp_save)
584
585 void
586 longjmp (jmp_buf env, int val) { __longjmp (env[0].__jmpbuf, val); }
587 weak_symbol (longjmp)
588
589
590 /* This function is called by interruptible RPC stubs. For initial
591 dynamic linking, just use the normal mach_msg. Since this defn is
592 weak, the real defn in libc.so will override it if we are linked into
593 the user program (-ldl). */
594
595 error_t
596 _hurd_intr_rpc_mach_msg (mach_msg_header_t *msg,
597 mach_msg_option_t option,
598 mach_msg_size_t send_size,
599 mach_msg_size_t rcv_size,
600 mach_port_t rcv_name,
601 mach_msg_timeout_t timeout,
602 mach_port_t notify)
603 {
604 return __mach_msg (msg, option, send_size, rcv_size, rcv_name,
605 timeout, notify);
606 }
607 weak_symbol (_hurd_intr_rpc_mach_msg)