Update.
[glibc.git] / sysdeps / generic / dl-sysdep.c
blob15f85550fe02567ed82ba97d99af627ec596c3e6
1 /* Operating system support for run-time dynamic linker. Generic Unix version.
2 Copyright (C) 1995, 1996, 1997, 1998 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 not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <elf.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/mman.h>
29 #include <elf/ldsodefs.h>
30 #include <stdio-common/_itoa.h>
32 #include <entry.h>
33 #include <dl-machine.h>
34 #include <dl-procinfo.h>
36 extern int _dl_argc;
37 extern char **_dl_argv;
38 extern char **_environ;
39 extern size_t _dl_pagesize;
40 extern const char *_dl_platform;
41 extern unsigned long int _dl_hwcap;
42 extern size_t _dl_platformlen;
43 extern void _end;
44 extern void ENTRY_POINT (void);
46 ElfW(Addr) _dl_base_addr;
47 int __libc_enable_secure;
48 int __libc_multiple_libcs; /* Defining this here avoids the inclusion
49 of init-first. */
50 /* This variable contains the lowest stack address ever used. */
51 void *__libc_stack_end;
52 static ElfW(auxv_t) *_dl_auxv;
53 unsigned long int _dl_hwcap_mask = HWCAP_IMPORTANT;
56 #ifndef DL_FIND_ARG_COMPONENTS
57 #define DL_FIND_ARG_COMPONENTS(cookie, argc, argv, envp, auxp) \
58 do { \
59 void **_tmp; \
60 (argc) = *(long *) cookie; \
61 (argv) = (char **) cookie + 1; \
62 (envp) = (argv) + (argc) + 1; \
63 for (_tmp = (void **) (envp); *_tmp; ++_tmp) \
64 continue; \
65 (auxp) = (void *) ++_tmp; \
66 } while (0)
67 #endif
70 ElfW(Addr)
71 _dl_sysdep_start (void **start_argptr,
72 void (*dl_main) (const ElfW(Phdr) *phdr, ElfW(Word) phnum,
73 ElfW(Addr) *user_entry))
75 const ElfW(Phdr) *phdr = NULL;
76 ElfW(Word) phnum = 0;
77 ElfW(Addr) user_entry;
78 ElfW(auxv_t) *av;
79 uid_t uid = 0;
80 uid_t euid = 0;
81 gid_t gid = 0;
82 gid_t egid = 0;
83 unsigned int seen;
85 DL_FIND_ARG_COMPONENTS (start_argptr, _dl_argc, _dl_argv, _environ, _dl_auxv);
87 user_entry = (ElfW(Addr)) &ENTRY_POINT;
88 _dl_platform = NULL; /* Default to nothing known about the platform. */
90 seen = 0;
91 #define M(type) (1 << (type))
93 for (av = _dl_auxv; av->a_type != AT_NULL; seen |= M ((++av)->a_type))
94 switch (av->a_type)
96 case AT_PHDR:
97 phdr = av->a_un.a_ptr;
98 break;
99 case AT_PHNUM:
100 phnum = av->a_un.a_val;
101 break;
102 case AT_PAGESZ:
103 _dl_pagesize = av->a_un.a_val;
104 break;
105 case AT_ENTRY:
106 user_entry = av->a_un.a_val;
107 break;
108 case AT_BASE:
109 _dl_base_addr = av->a_un.a_val;
110 break;
111 case AT_UID:
112 uid = av->a_un.a_val;
113 break;
114 case AT_GID:
115 gid = av->a_un.a_val;
116 break;
117 case AT_EUID:
118 euid = av->a_un.a_val;
119 break;
120 case AT_EGID:
121 egid = av->a_un.a_val;
122 break;
123 case AT_PLATFORM:
124 _dl_platform = av->a_un.a_ptr;
125 break;
126 case AT_HWCAP:
127 _dl_hwcap = av->a_un.a_val;
128 break;
131 /* Linux doesn't provide us with any of these values on the stack
132 when the dynamic linker is run directly as a program. */
134 #define SEE(UID, uid) if ((seen & M (AT_##UID)) == 0) uid = __get##uid ()
135 SEE (UID, uid);
136 SEE (GID, gid);
137 SEE (EUID, euid);
138 SEE (EGID, egid);
140 __libc_enable_secure = uid != euid || gid != egid;
142 if (_dl_pagesize == 0)
143 _dl_pagesize = __getpagesize ();
145 #ifdef DL_SYSDEP_INIT
146 DL_SYSDEP_INIT;
147 #endif
149 #ifdef DL_PLATFORM_INIT
150 DL_PLATFORM_INIT;
151 #endif
153 /* Determine the length of the platform name. */
154 if (_dl_platform != NULL)
155 _dl_platformlen = strlen (_dl_platform);
157 if (__sbrk (0) == &_end)
158 /* The dynamic linker was run as a program, and so the initial break
159 starts just after our bss, at &_end. The malloc in dl-minimal.c
160 will consume the rest of this page, so tell the kernel to move the
161 break up that far. When the user program examines its break, it
162 will see this new value and not clobber our data. */
163 __sbrk (_dl_pagesize - ((&_end - (void *) 0) & (_dl_pagesize - 1)));
165 (*dl_main) (phdr, phnum, &user_entry);
166 return user_entry;
169 void
170 _dl_sysdep_start_cleanup (void)
174 void
175 internal_function
176 _dl_show_auxv (void)
178 char buf[64];
179 ElfW(auxv_t) *av;
181 /* Terminate string. */
182 buf[63] = '\0';
184 for (av = _dl_auxv; av->a_type != AT_NULL; ++av)
185 switch (av->a_type)
187 case AT_PHDR:
188 _dl_sysdep_message ("AT_PHDR: 0x",
189 _itoa_word (av->a_un.a_val, buf + sizeof buf - 1,
190 16, 0),
191 "\n", NULL);
192 break;
193 case AT_PHNUM:
194 _dl_sysdep_message ("AT_PHNUM: ",
195 _itoa_word (av->a_un.a_val, buf + sizeof buf - 1,
196 10, 0),
197 "\n", NULL);
198 break;
199 case AT_PAGESZ:
200 _dl_sysdep_message ("AT_PAGESZ: ",
201 _itoa_word (av->a_un.a_val, buf + sizeof buf - 1,
202 10, 0),
203 "\n", NULL);
204 break;
205 case AT_ENTRY:
206 _dl_sysdep_message ("AT_ENTRY: 0x",
207 _itoa_word (av->a_un.a_val, buf + sizeof buf - 1,
208 16, 0),
209 "\n", NULL);
210 break;
211 case AT_BASE:
212 _dl_sysdep_message ("AT_BASE: 0x",
213 _itoa_word (av->a_un.a_val, buf + sizeof buf - 1,
214 16, 0),
215 "\n", NULL);
216 break;
217 case AT_UID:
218 _dl_sysdep_message ("AT_UID: ",
219 _itoa_word (av->a_un.a_val, buf + sizeof buf - 1,
220 10, 0),
221 "\n", NULL);
222 break;
223 case AT_GID:
224 _dl_sysdep_message ("AT_GID: ",
225 _itoa_word (av->a_un.a_val, buf + sizeof buf - 1,
226 10, 0),
227 "\n", NULL);
228 break;
229 case AT_EUID:
230 _dl_sysdep_message ("AT_EUID: ",
231 _itoa_word (av->a_un.a_val, buf + sizeof buf - 1,
232 10, 0),
233 "\n", NULL);
234 break;
235 case AT_EGID:
236 _dl_sysdep_message ("AT_EGID: ",
237 _itoa_word (av->a_un.a_val, buf + sizeof buf - 1,
238 10, 0),
239 "\n", NULL);
240 break;
241 case AT_PLATFORM:
242 _dl_sysdep_message ("AT_PLATFORM: ", av->a_un.a_ptr, "\n", NULL);
243 break;
244 case AT_HWCAP:
245 _dl_hwcap = av->a_un.a_val;
246 if (_dl_procinfo (_dl_hwcap) < 0)
247 _dl_sysdep_message ("AT_HWCAP: ",
248 _itoa_word (_dl_hwcap, buf + sizeof buf - 1,
249 16, 0),
250 "\n", NULL);
251 break;
256 /* Return an array of useful/necessary hardware capability names. */
257 const struct r_strlenpair *
258 internal_function
259 _dl_important_hwcaps (const char *platform, size_t platform_len, size_t *sz,
260 size_t *max_capstrlen)
262 /* Determine how many important bits are set. */
263 unsigned long int masked = _dl_hwcap & _dl_hwcap_mask;
264 size_t cnt = platform != NULL;
265 size_t n, m;
266 size_t total;
267 struct r_strlenpair *temp;
268 struct r_strlenpair *result;
269 struct r_strlenpair *rp;
270 char *cp;
272 /* Count the number of bits set in the masked value. */
273 for (n = 0; (~((1UL << n) - 1) & masked) != 0; ++n)
274 if ((masked & (1UL << n)) != 0)
275 ++cnt;
277 if (cnt == 0)
279 /* If we have platform name and no important capability we only have
280 the base directory to search. */
281 result = (struct r_strlenpair *) malloc (sizeof (*result));
282 if (result == NULL)
284 no_memory:
285 _dl_signal_error (ENOMEM, NULL, "cannot create capability list");
288 result[0].str = (char *) result; /* Does not really matter. */
289 result[0].len = 0;
291 *sz = 1;
292 return result;
295 /* Create temporary data structure to generate result table. */
296 temp = (struct r_strlenpair *) alloca (cnt * sizeof (*temp));
297 m = 0;
298 for (n = 0; masked != 0; ++n)
299 if ((masked & (1UL << n)) != 0)
301 temp[m].str = _dl_hwcap_string (n);
302 temp[m].len = strlen (temp[m].str);
303 masked ^= 1UL << n;
304 ++m;
306 if (platform != NULL)
308 temp[m].str = platform;
309 temp[m].len = platform_len;
310 ++m;
313 /* Determine the total size of all strings together. */
314 if (cnt == 1)
315 total = temp[0].len;
316 else
318 total = (1 << (cnt - 2)) * (temp[0].len + temp[cnt - 1].len + 2);
319 for (n = 1; n + 1 < cnt; ++n)
320 total += (1 << (cnt - 3)) * (temp[n].len + 1);
323 /* The result structure: we use a very compressed way to store the
324 various combinations of capability names. */
325 *sz = 1 << cnt;
326 result = (struct r_strlenpair *) malloc (*sz * sizeof (*result) + total);
327 if (result == NULL)
328 goto no_memory;
330 if (cnt == 1)
332 result[0].str = (char *) (result + *sz);
333 result[0].len = temp[0].len + 1;
334 result[1].str = (char *) (result + *sz);
335 result[1].len = 0;
336 cp = __mempcpy ((char *) (result + *sz), temp[0].str, temp[0].len);
337 *cp = '/';
338 *sz = 2;
339 *max_capstrlen = result[0].len;
341 return result;
344 /* Fill in the information. This follows the following scheme
345 (indeces from TEMP for four strings):
346 entry #0: 0, 1, 2, 3 binary: 1111
347 #1: 0, 1, 3 1101
348 #2: 0, 2, 3 1011
349 #3: 0, 3 1001
350 This allows to represent all possible combinations of capability
351 names in the string. First generate the strings. */
352 result[1].str = result[0].str = cp = (char *) (result + *sz);
353 #define add(idx) \
354 cp = __mempcpy (__mempcpy (cp, temp[idx].str, temp[idx].len), "/", 1);
355 if (cnt == 2)
357 add (1);
358 add (0);
360 else
362 n = 1 << cnt;
365 n -= 2;
367 /* We always add the last string. */
368 add (cnt - 1);
370 /* Add the strings which have the bit set in N. */
371 for (m = cnt - 2; m > 0; --m)
372 if ((n & (1 << m)) != 0)
373 add (m);
375 /* Always add the first string. */
376 add (0);
378 while (n != 0);
380 #undef add
382 /* Now we are ready to install the string pointers and length. */
383 for (n = 0; n < (1 << cnt); ++n)
384 result[n].len = 0;
385 n = cnt;
388 size_t mask = 1 << --n;
390 rp = result;
391 for (m = 1 << cnt; m > 0; ++rp)
392 if ((--m & mask) != 0)
393 rp->len += temp[n].len + 1;
395 while (n != 0);
397 /* The first half of the strings all include the first string. */
398 n = (1 << cnt) - 2;
399 rp = &result[2];
400 while (n != (1 << (cnt - 1)))
402 if ((n & 1) != 0)
403 rp[0].str = rp[-2].str + rp[-2].len;
404 else
405 rp[0].str = rp[-1].str;
406 ++rp;
407 --n;
410 /* The second have starts right after the first part of the string of
411 corresponding entry in the first half. */
414 rp[0].str = rp[-(1 << (cnt - 1))].str + temp[cnt - 1].len + 1;
415 ++rp;
417 while (--n != 0);
419 /* The maximum string length. */
420 *max_capstrlen = result[0].len;
422 return result;