]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/generic/dl-sysdep.c
update from main archive 970209
[thirdparty/glibc.git] / sysdeps / generic / dl-sysdep.c
1 /* Operating system support for run-time dynamic linker. Generic Unix version.
2 Copyright (C) 1995, 1996, 1997 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 not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19
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>
29
30
31 extern int _dl_argc;
32 extern char **_dl_argv;
33 extern char **_environ;
34 extern size_t _dl_pagesize;
35 extern void _end;
36 extern void _start (void);
37
38 int __libc_enable_secure;
39 int __libc_multiple_libcs; /* Defining this here avoids the inclusion
40 of init-first. */
41
42 ElfW(Addr)
43 _dl_sysdep_start (void **start_argptr,
44 void (*dl_main) (const ElfW(Phdr) *phdr, ElfW(Word) phnum,
45 ElfW(Addr) *user_entry))
46 {
47 const ElfW(Phdr) *phdr = NULL;
48 ElfW(Word) phnum = 0;
49 ElfW(Addr) user_entry;
50 ElfW(auxv_t) *av;
51 uid_t uid = 0;
52 uid_t euid = 0;
53 gid_t gid = 0;
54 gid_t egid = 0;
55 unsigned int seen;
56
57 user_entry = (ElfW(Addr)) &_start;
58 _dl_argc = *(long *) start_argptr;
59 _dl_argv = (char **) start_argptr + 1;
60 _environ = &_dl_argv[_dl_argc + 1];
61 start_argptr = (void **) _environ;
62 while (*start_argptr)
63 ++start_argptr;
64
65 seen = 0;
66 #define M(type) (1 << (type))
67
68 for (av = (void *) ++start_argptr;
69 av->a_type != AT_NULL;
70 seen |= M ((++av)->a_type))
71 switch (av->a_type)
72 {
73 case AT_PHDR:
74 phdr = av->a_un.a_ptr;
75 break;
76 case AT_PHNUM:
77 phnum = av->a_un.a_val;
78 break;
79 case AT_PAGESZ:
80 _dl_pagesize = av->a_un.a_val;
81 break;
82 case AT_ENTRY:
83 user_entry = av->a_un.a_val;
84 break;
85 case AT_UID:
86 uid = av->a_un.a_val;
87 break;
88 case AT_GID:
89 gid = av->a_un.a_val;
90 break;
91 case AT_EUID:
92 euid = av->a_un.a_val;
93 break;
94 case AT_EGID:
95 egid = av->a_un.a_val;
96 break;
97 }
98
99 /* Linux doesn't provide us with any of these values on the stack
100 when the dynamic linker is run directly as a program. */
101
102 #define SEE(UID, uid) if ((seen & M (AT_##UID)) == 0) uid = __get##uid ()
103 SEE (UID, uid);
104 SEE (GID, gid);
105 SEE (EUID, euid);
106 SEE (EGID, egid);
107
108
109 __libc_enable_secure = uid != euid || gid != egid;
110
111 #ifdef DL_SYSDEP_INIT
112 DL_SYSDEP_INIT;
113 #endif
114
115 if (__sbrk (0) == &_end)
116 {
117 /* The dynamic linker was run as a program, and so the initial break
118 starts just after our bss, at &_end. The malloc in dl-minimal.c
119 will consume the rest of this page, so tell the kernel to move the
120 break up that far. When the user program examines its break, it
121 will see this new value and not clobber our data. */
122 size_t pg = __getpagesize ();
123
124 __sbrk (pg - ((&_end - (void *) 0) & (pg - 1)));
125 }
126
127 (*dl_main) (phdr, phnum, &user_entry);
128 return user_entry;
129 }
130
131 void
132 _dl_sysdep_start_cleanup (void)
133 {
134 }
135 \f
136 #ifndef MAP_ANON
137 /* This is only needed if the system doesn't support MAP_ANON. */
138
139 int
140 _dl_sysdep_open_zero_fill (void)
141 {
142 return __open ("/dev/zero", O_RDONLY);
143 }
144 #endif
145
146 /* Read the whole contents of FILE into new mmap'd space with given
147 protections. *SIZEP gets the size of the file. */
148
149 void *
150 _dl_sysdep_read_whole_file (const char *file, size_t *sizep, int prot)
151 {
152 void *result;
153 struct stat st;
154 int fd = __open (file, O_RDONLY);
155 if (fd < 0)
156 return NULL;
157 if (__fstat (fd, &st) < 0)
158 result = NULL;
159 else
160 {
161 /* Map a copy of the file contents. */
162 result = __mmap (0, st.st_size, prot,
163 #ifdef MAP_COPY
164 MAP_COPY
165 #else
166 MAP_PRIVATE
167 #endif
168 #ifdef MAP_FILE
169 | MAP_FILE
170 #endif
171 , fd, 0);
172 if (result == (void *) -1)
173 result = NULL;
174 else
175 *sizep = st.st_size;
176 }
177 __close (fd);
178 return result;
179 }
180
181 void
182 _dl_sysdep_fatal (const char *msg, ...)
183 {
184 va_list ap;
185
186 va_start (ap, msg);
187 do
188 {
189 size_t len = strlen (msg);
190 __write (STDERR_FILENO, msg, len);
191 msg = va_arg (ap, const char *);
192 } while (msg);
193 va_end (ap);
194
195 _exit (127);
196 }
197
198
199 void
200 _dl_sysdep_error (const char *msg, ...)
201 {
202 va_list ap;
203
204 va_start (ap, msg);
205 do
206 {
207 size_t len = strlen (msg);
208 __write (STDERR_FILENO, msg, len);
209 msg = va_arg (ap, const char *);
210 } while (msg);
211 va_end (ap);
212 }
213
214
215 void
216 _dl_sysdep_message (const char *msg, ...)
217 {
218 va_list ap;
219
220 va_start (ap, msg);
221 do
222 {
223 size_t len = strlen (msg);
224 __write (STDOUT_FILENO, msg, len);
225 msg = va_arg (ap, const char *);
226 } while (msg);
227 va_end (ap);
228 }