Fri Aug 2 01:04:02 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc.git] / sysdeps / generic / dl-sysdep.c
blob36349a5658dc63a7c845c9fa1a1def9096c5ac11
1 /* Operating system support for run-time dynamic linker. Generic Unix version.
2 Copyright (C) 1995, 96 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 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.
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.
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. */
20 #include <elf.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/mman.h>
24 #include <fcntl.h>
25 #include <link.h>
26 #include <unistd.h>
27 #include <stdarg.h>
28 #include <string.h>
31 extern int _dl_argc;
32 extern char **_dl_argv;
33 extern char **_environ;
34 extern void _start (void);
36 int __libc_enable_secure;
38 ElfW(Addr)
39 _dl_sysdep_start (void **start_argptr,
40 void (*dl_main) (const ElfW(Phdr) *phdr, ElfW(Word) phnum,
41 ElfW(Addr) *user_entry))
43 const ElfW(Phdr) *phdr;
44 ElfW(Word) phnum;
45 ElfW(Addr) user_entry;
46 ElfW(auxv_t) *av;
47 uid_t uid, euid;
48 gid_t gid, egid;
49 unsigned int seen;
51 user_entry = (ElfW(Addr)) &_start;
52 _dl_argc = *(long *) start_argptr;
53 _dl_argv = (char **) start_argptr + 1;
54 _environ = &_dl_argv[_dl_argc + 1];
55 start_argptr = (void **) _environ;
56 while (*start_argptr)
57 ++start_argptr;
59 seen = 0;
60 #define M(type) (1 << (type))
62 for (av = (void *) ++start_argptr;
63 av->a_type != AT_NULL;
64 seen |= M ((++av)->a_type))
65 switch (av->a_type)
67 case AT_PHDR:
68 phdr = av->a_un.a_ptr;
69 break;
70 case AT_PHNUM:
71 phnum = av->a_un.a_val;
72 break;
73 case AT_ENTRY:
74 user_entry = av->a_un.a_val;
75 break;
76 case AT_UID:
77 uid = av->a_un.a_val;
78 break;
79 case AT_GID:
80 gid = av->a_un.a_val;
81 break;
82 case AT_EUID:
83 euid = av->a_un.a_val;
84 break;
85 case AT_EGID:
86 egid = av->a_un.a_val;
87 break;
90 /* Linux doesn't provide us with any of these values on the stack
91 when the dynamic linker is run directly as a program. */
93 #define SEE(UID, uid) if ((seen & M (AT_##UID)) == 0) uid = __get##uid ()
94 SEE (UID, uid);
95 SEE (GID, gid);
96 SEE (EUID, euid);
97 SEE (EGID, egid);
100 __libc_enable_secure = uid != euid || gid != egid;
102 #ifdef DL_SYSDEP_INIT
103 DL_SYSDEP_INIT;
104 #endif
106 (*dl_main) (phdr, phnum, &user_entry);
107 return user_entry;
110 void
111 _dl_sysdep_start_cleanup (void)
115 #ifndef MAP_ANON
116 /* This is only needed if the system doesn't support MAP_ANON. */
119 _dl_sysdep_open_zero_fill (void)
121 return __open ("/dev/zero", O_RDONLY);
123 #endif
125 /* Read the whole contents of FILE into new mmap'd space with given
126 protections. *SIZEP gets the size of the file. */
128 void *
129 _dl_sysdep_read_whole_file (const char *file, size_t *sizep, int prot)
131 void *result;
132 struct stat st;
133 int fd = __open (file, O_RDONLY);
134 if (fd < 0)
135 return NULL;
136 if (__fstat (fd, &st) < 0)
137 result = NULL;
138 else
140 /* Map a copy of the file contents. */
141 result = __mmap (0, st.st_size, prot,
142 #ifdef MAP_COPY
143 MAP_COPY
144 #else
145 MAP_PRIVATE
146 #endif
147 #ifdef MAP_FILE
148 | MAP_FILE
149 #endif
150 , fd, 0);
151 if (result == (void *) -1)
152 result = NULL;
153 else
154 *sizep = st.st_size;
156 __close (fd);
157 return result;
160 void
161 _dl_sysdep_fatal (const char *msg, ...)
163 va_list ap;
165 va_start (ap, msg);
168 size_t len = strlen (msg);
169 __write (STDERR_FILENO, msg, len);
170 msg = va_arg (ap, const char *);
171 } while (msg);
172 va_end (ap);
174 _exit (127);
178 void
179 _dl_sysdep_message (const char *msg, ...)
181 va_list ap;
183 va_start (ap, msg);
186 size_t len = strlen (msg);
187 __write (STDOUT_FILENO, msg, len);
188 msg = va_arg (ap, const char *);
189 } while (msg);
190 va_end (ap);