]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/dl-load.c
Update.
[thirdparty/glibc.git] / elf / dl-load.c
CommitLineData
0a54e401 1/* Map in a shared object's segments from the file.
880f421f 2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
afd4eb37 3 This file is part of the GNU C Library.
d66e34cd 4
afd4eb37
UD
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.
d66e34cd 9
afd4eb37
UD
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.
d66e34cd 14
afd4eb37
UD
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. */
d66e34cd 19
14e9dd67 20#include <elf.h>
0a54e401
UD
21#include <errno.h>
22#include <fcntl.h>
0a54e401 23#include <stdlib.h>
d66e34cd 24#include <string.h>
d66e34cd 25#include <unistd.h>
a853022c 26#include <elf/ldsodefs.h>
0a54e401
UD
27#include <sys/mman.h>
28#include <sys/stat.h>
29#include <sys/types.h>
d66e34cd 30#include "dynamic-link.h"
8193034b 31#include <stdio-common/_itoa.h>
d66e34cd
RM
32
33
9b8a44cd
RM
34/* On some systems, no flag bits are given to specify file mapping. */
35#ifndef MAP_FILE
36#define MAP_FILE 0
37#endif
38
39/* The right way to map in the shared library files is MAP_COPY, which
40 makes a virtual copy of the data at the time of the mmap call; this
41 guarantees the mapped pages will be consistent even if the file is
42 overwritten. Some losing VM systems like Linux's lack MAP_COPY. All we
43 get is MAP_PRIVATE, which copies each page when it is modified; this
44 means if the file is overwritten, we may at some point get some pages
45 from the new version after starting with pages from the old version. */
46#ifndef MAP_COPY
47#define MAP_COPY MAP_PRIVATE
48#endif
49
f21acc89
UD
50/* Some systems link their relocatable objects for another base address
51 than 0. We want to know the base address for these such that we can
52 subtract this address from the segment addresses during mapping.
53 This results in a more efficient address space usage. Defaults to
54 zero for almost all systems. */
55#ifndef MAP_BASE_ADDR
56#define MAP_BASE_ADDR(l) 0
57#endif
58
9b8a44cd 59
d66e34cd
RM
60#include <endian.h>
61#if BYTE_ORDER == BIG_ENDIAN
62#define byteorder ELFDATA2MSB
63#define byteorder_name "big-endian"
64#elif BYTE_ORDER == LITTLE_ENDIAN
65#define byteorder ELFDATA2LSB
66#define byteorder_name "little-endian"
67#else
68#error "Unknown BYTE_ORDER " BYTE_ORDER
69#define byteorder ELFDATANONE
70#endif
71
14e9dd67 72#define STRING(x) __STRING (x)
d66e34cd 73
2064087b
RM
74#ifdef MAP_ANON
75/* The fd is not examined when using MAP_ANON. */
76#define ANONFD -1
77#else
d66e34cd 78int _dl_zerofd = -1;
2064087b
RM
79#define ANONFD _dl_zerofd
80#endif
81
4cca6b86
UD
82/* Handle situations where we have a preferred location in memory for
83 the shared objects. */
84#ifdef ELF_PREFERRED_ADDRESS_DATA
85ELF_PREFERRED_ADDRESS_DATA;
86#endif
87#ifndef ELF_PREFERRED_ADDRESS
88#define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
89#endif
90#ifndef ELF_FIXED_ADDRESS
91#define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
92#endif
93
266180eb 94size_t _dl_pagesize;
d66e34cd 95
0a54e401
UD
96extern const char *_dl_platform;
97extern size_t _dl_platformlen;
d66e34cd 98
40a55d20
UD
99/* This is a fake list to store the RPATH information for static
100 binaries. */
101static struct r_search_path_elem **fake_path_list;
102
12264bd7
UD
103/* List of the hardware capabilities we might end up using. */
104static const struct r_strlenpair *capstr;
105static size_t ncapstr;
106static size_t max_capstrlen;
107
40a55d20 108
706074a5
UD
109/* Local version of `strdup' function. */
110static inline char *
111local_strdup (const char *s)
112{
113 size_t len = strlen (s) + 1;
114 void *new = malloc (len);
115
116 if (new == NULL)
117 return NULL;
118
119 return (char *) memcpy (new, s, len);
120}
121
0413b54c
UD
122/* Add `name' to the list of names for a particular shared object.
123 `name' is expected to have been allocated with malloc and will
124 be freed if the shared object already has this name.
125 Returns false if the object already had this name. */
126static int
12264bd7 127internal_function
0413b54c 128add_name_to_object (struct link_map *l, char *name)
0a54e401 129{
0413b54c
UD
130 struct libname_list *lnp, *lastp;
131 struct libname_list *newname;
0a54e401 132
0413b54c 133 if (name == NULL)
da832465
UD
134 {
135 /* No more memory. */
136 _dl_signal_error (ENOMEM, NULL, "could not allocate name string");
137 return 0;
138 }
0413b54c
UD
139
140 lastp = NULL;
141 for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next)
142 if (strcmp (name, lnp->name) == 0)
143 {
144 free (name);
145 return 0;
146 }
147
148 newname = malloc (sizeof *newname);
149 if (newname == NULL)
da832465
UD
150 {
151 /* No more memory. */
152 _dl_signal_error (ENOMEM, name, "cannot allocate name record");
153 free (name);
154 return 0;
155 }
0413b54c
UD
156 /* The object should have a libname set from _dl_new_object. */
157 assert (lastp != NULL);
158
159 newname->name = name;
160 newname->next = NULL;
161 lastp->next = newname;
162 return 1;
163}
164
12264bd7
UD
165/* All known directories in sorted order. */
166static struct r_search_path_elem *all_dirs;
0413b54c 167
12264bd7
UD
168/* Standard search directories. */
169static struct r_search_path_elem **rtld_search_dirs;
0a54e401
UD
170
171static size_t max_dirnamelen;
172
173static inline struct r_search_path_elem **
174fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
b5efde2f 175 const char **trusted, const char *what, const char *where)
0a54e401
UD
176{
177 char *cp;
178 size_t nelems = 0;
179
180 while ((cp = __strsep (&rpath, sep)) != NULL)
181 {
182 struct r_search_path_elem *dirp;
183 size_t len = strlen (cp);
12264bd7
UD
184
185 /* `strsep' can pass an empty string. */
186 if (len == 0)
187 continue;
188
0a54e401
UD
189 /* Remove trailing slashes. */
190 while (len > 1 && cp[len - 1] == '/')
191 --len;
192
193 /* Make sure we don't use untrusted directories if we run SUID. */
194 if (trusted != NULL)
195 {
196 const char **trun = trusted;
197
198 /* All trusted directory must be complete name. */
199 if (cp[0] != '/')
200 continue;
201
202 while (*trun != NULL
12264bd7
UD
203 && (memcmp (*trun, cp, len) != 0
204 || ((*trun)[len] != '/' && (*trun)[len + 1] != '\0')))
0a54e401
UD
205 ++trun;
206
207 if (*trun == NULL)
208 /* It's no trusted directory, skip it. */
209 continue;
210 }
211
212 /* Now add one. */
213 if (len > 0)
214 cp[len++] = '/';
215
216 /* See if this directory is already known. */
217 for (dirp = all_dirs; dirp != NULL; dirp = dirp->next)
12264bd7 218 if (dirp->dirnamelen == len && memcmp (cp, dirp->dirname, len) == 0)
0a54e401
UD
219 break;
220
221 if (dirp != NULL)
222 {
12264bd7 223 /* It is available, see whether it's on our own list. */
0a54e401
UD
224 size_t cnt;
225 for (cnt = 0; cnt < nelems; ++cnt)
226 if (result[cnt] == dirp)
227 break;
228
229 if (cnt == nelems)
230 result[nelems++] = dirp;
231 }
232 else
233 {
12264bd7
UD
234 size_t cnt;
235
0a54e401 236 /* It's a new directory. Create an entry and add it. */
12264bd7
UD
237 dirp = (struct r_search_path_elem *)
238 malloc (sizeof (*dirp) + ncapstr * sizeof (enum r_dir_status));
0a54e401
UD
239 if (dirp == NULL)
240 _dl_signal_error (ENOMEM, NULL,
241 "cannot create cache for search path");
242
12264bd7 243 dirp->dirname = cp;
0a54e401 244 dirp->dirnamelen = len;
12264bd7
UD
245
246 if (len > max_dirnamelen)
247 max_dirnamelen = len;
248
3996f34b
UD
249 /* We have to make sure all the relative directories are never
250 ignored. The current directory might change and all our
251 saved information would be void. */
12264bd7
UD
252 if (cp[0] != '/')
253 for (cnt = 0; cnt < ncapstr; ++cnt)
254 dirp->status[cnt] = existing;
0a54e401 255 else
12264bd7
UD
256 for (cnt = 0; cnt < ncapstr; ++cnt)
257 dirp->status[cnt] = unknown;
0a54e401 258
b5efde2f
UD
259 dirp->what = what;
260 dirp->where = where;
261
0a54e401
UD
262 dirp->next = all_dirs;
263 all_dirs = dirp;
264
265 /* Put it in the result array. */
266 result[nelems++] = dirp;
267 }
268 }
269
270 /* Terminate the array. */
271 result[nelems] = NULL;
272
273 return result;
274}
275
276
277static struct r_search_path_elem **
12264bd7 278internal_function
b5efde2f
UD
279decompose_rpath (const char *rpath, size_t additional_room,
280 const char *what, const char *where)
0a54e401
UD
281{
282 /* Make a copy we can work with. */
4bca4c17 283 char *copy = local_strdup (rpath);
0a54e401
UD
284 char *cp;
285 struct r_search_path_elem **result;
286 /* First count the number of necessary elements in the result array. */
287 size_t nelems = 0;
288
289 for (cp = copy; *cp != '\0'; ++cp)
290 if (*cp == ':')
291 ++nelems;
292
293 /* Allocate room for the result. NELEMS + 1 + ADDITIONAL_ROOM is an upper
294 limit for the number of necessary entries. */
295 result = (struct r_search_path_elem **) malloc ((nelems + 1
296 + additional_room + 1)
297 * sizeof (*result));
298 if (result == NULL)
299 _dl_signal_error (ENOMEM, NULL, "cannot create cache for search path");
300
b5efde2f 301 return fillin_rpath (copy, result, ":", NULL, what, where);
0a54e401
UD
302}
303
304
305void
d0fc4041 306internal_function
880f421f 307_dl_init_paths (const char *llp)
0a54e401 308{
12264bd7 309 static const char *system_dirs[] =
40a55d20
UD
310 {
311#include "trusted-dirs.h"
312 NULL
313 };
12264bd7
UD
314 const char **strp;
315 struct r_search_path_elem *pelem, **aelem;
316 size_t round_size;
0a54e401
UD
317
318 /* We have in `search_path' the information about the RPATH of the
319 dynamic loader. Now fill in the information about the applications
320 RPATH and the directories addressed by the LD_LIBRARY_PATH environment
321 variable. */
322 struct link_map *l;
323
880f421f 324 /* Number of elements in the library path. */
0a54e401
UD
325 size_t nllp;
326
880f421f 327 /* First determine how many elements the LD_LIBRARY_PATH contents has. */
0a54e401
UD
328 if (llp != NULL && *llp != '\0')
329 {
330 /* Simply count the number of colons. */
331 const char *cp = llp;
332 nllp = 1;
333 while (*cp)
334 if (*cp++ == ':')
335 ++nllp;
336 }
337 else
338 nllp = 0;
339
4317f9e1 340 /* Get the capabilities. */
12264bd7
UD
341 capstr = _dl_important_hwcaps (_dl_platform, _dl_platformlen,
342 &ncapstr, &max_capstrlen);
343
344 /* First set up the rest of the default search directory entries. */
345 aelem = rtld_search_dirs = (struct r_search_path_elem **)
346 malloc ((ncapstr + 1) * sizeof (struct r_search_path_elem *));
347
348 round_size = ((2 * sizeof (struct r_search_path_elem) - 1
349 + ncapstr * sizeof (enum r_dir_status))
350 / sizeof (struct r_search_path_elem));
351
352 rtld_search_dirs[0] = (struct r_search_path_elem *)
353 malloc ((sizeof (system_dirs) / sizeof (system_dirs[0]) - 1)
354 * round_size * sizeof (struct r_search_path_elem));
355 if (rtld_search_dirs[0] == NULL)
356 _dl_signal_error (ENOMEM, NULL, "cannot create cache for search path");
357
358 pelem = all_dirs= rtld_search_dirs[0];
359 for (strp = system_dirs; *strp != NULL; ++strp, pelem += round_size)
360 {
361 size_t cnt;
362
363 *aelem++ = pelem;
364
365 pelem->next = *(strp + 1) == NULL ? NULL : (pelem + round_size);
366
367 pelem->what = "system search path";
368 pelem->where = NULL;
369
370 pelem->dirnamelen = strlen (pelem->dirname = *strp);
371 if (pelem->dirnamelen > max_dirnamelen)
372 max_dirnamelen = pelem->dirnamelen;
373
374 if (pelem->dirname[0] != '/')
375 for (cnt = 0; cnt < ncapstr; ++cnt)
376 pelem->status[cnt] = existing;
377 else
378 for (cnt = 0; cnt < ncapstr; ++cnt)
379 pelem->status[cnt] = unknown;
380 }
381 *aelem = NULL;
4317f9e1 382
0a54e401 383 l = _dl_loaded;
40a55d20 384 if (l != NULL)
0a54e401 385 {
40a55d20
UD
386 if (l->l_type != lt_loaded && l->l_info[DT_RPATH])
387 {
388 /* Allocate room for the search path and fill in information
389 from RPATH. */
390 l->l_rpath_dirs =
391 decompose_rpath ((const char *)
392 (l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr
393 + l->l_info[DT_RPATH]->d_un.d_val),
b5efde2f 394 nllp, "RPATH", l->l_name);
40a55d20
UD
395 }
396 else
397 {
398 /* If we have no LD_LIBRARY_PATH and no RPATH we must tell
399 this somehow to prevent we look this up again and again. */
400 if (nllp == 0)
401 l->l_rpath_dirs = (struct r_search_path_elem **) -1l;
402 else
403 {
404 l->l_rpath_dirs = (struct r_search_path_elem **)
405 malloc ((nllp + 1) * sizeof (*l->l_rpath_dirs));
406 if (l->l_rpath_dirs == NULL)
407 _dl_signal_error (ENOMEM, NULL,
408 "cannot create cache for search path");
409 l->l_rpath_dirs[0] = NULL;
410 }
411 }
412
413 /* We don't need to search the list of fake entries which is searched
414 when no dynamic objects were loaded at this time. */
415 fake_path_list = NULL;
416
417 if (nllp > 0)
418 {
12264bd7 419 char *copy = local_strdup (llp);
40a55d20
UD
420
421 /* Decompose the LD_LIBRARY_PATH and fill in the result.
422 First search for the next place to enter elements. */
423 struct r_search_path_elem **result = l->l_rpath_dirs;
424 while (*result != NULL)
425 ++result;
426
427 /* We need to take care that the LD_LIBRARY_PATH environment
428 variable can contain a semicolon. */
429 (void) fillin_rpath (copy, result, ":;",
12264bd7 430 __libc_enable_secure ? system_dirs : NULL,
b5efde2f 431 "LD_LIBRARY_PATH", NULL);
40a55d20 432 }
0a54e401
UD
433 }
434 else
435 {
40a55d20
UD
436 /* This is a statically linked program but we still have to
437 take care for the LD_LIBRARY_PATH environment variable. We
438 use a fake link_map entry. This will only contain the
439 l_rpath_dirs information. */
440
0a54e401 441 if (nllp == 0)
40a55d20 442 fake_path_list = NULL;
0a54e401
UD
443 else
444 {
40a55d20
UD
445 fake_path_list = (struct r_search_path_elem **)
446 malloc ((nllp + 1) * sizeof (struct r_search_path_elem *));
f41c8091
UD
447 if (fake_path_list == NULL)
448 _dl_signal_error (ENOMEM, NULL,
449 "cannot create cache for search path");
0a54e401 450
40a55d20 451 (void) fillin_rpath (local_strdup (llp), fake_path_list, ":;",
12264bd7 452 __libc_enable_secure ? system_dirs : NULL,
b5efde2f 453 "LD_LIBRARY_PATH", NULL);
40a55d20 454 }
0a54e401 455 }
0a54e401
UD
456}
457
458
ea03559a
RM
459/* Map in the shared object NAME, actually located in REALNAME, and already
460 opened on FD. */
461
462struct link_map *
706074a5 463_dl_map_object_from_fd (char *name, int fd, char *realname,
ba79d61b 464 struct link_map *loader, int l_type)
ea03559a 465{
622586fb 466 struct link_map *l = NULL;
ea03559a
RM
467 void *file_mapping = NULL;
468 size_t mapping_size = 0;
469
b122c703 470#define LOSE(s) lose (0, (s))
ea03559a
RM
471 void lose (int code, const char *msg)
472 {
266180eb 473 (void) __close (fd);
ea03559a 474 if (file_mapping)
266180eb 475 __munmap (file_mapping, mapping_size);
ba79d61b
RM
476 if (l)
477 {
478 /* Remove the stillborn object from the list and free it. */
479 if (l->l_prev)
480 l->l_prev->l_next = l->l_next;
481 if (l->l_next)
482 l->l_next->l_prev = l->l_prev;
483 free (l);
484 }
485 free (realname);
486 _dl_signal_error (code, name, msg);
0413b54c
UD
487 free (name); /* Hmmm. Can this leak memory? Better
488 than a segfault, anyway. */
ea03559a
RM
489 }
490
266180eb 491 inline caddr_t map_segment (ElfW(Addr) mapstart, size_t len,
b122c703
RM
492 int prot, int fixed, off_t offset)
493 {
266180eb
RM
494 caddr_t mapat = __mmap ((caddr_t) mapstart, len, prot,
495 fixed|MAP_COPY|MAP_FILE,
496 fd, offset);
0413b54c 497 if (mapat == MAP_FAILED)
b122c703
RM
498 lose (errno, "failed to map segment from shared object");
499 return mapat;
500 }
501
ea03559a
RM
502 /* Make sure LOCATION is mapped in. */
503 void *map (off_t location, size_t size)
504 {
505 if ((off_t) mapping_size <= location + (off_t) size)
506 {
507 void *result;
508 if (file_mapping)
266180eb
RM
509 __munmap (file_mapping, mapping_size);
510 mapping_size = (location + size + 1 + _dl_pagesize - 1);
511 mapping_size &= ~(_dl_pagesize - 1);
512 result = __mmap (file_mapping, mapping_size, PROT_READ,
513 MAP_COPY|MAP_FILE, fd, 0);
0413b54c 514 if (result == MAP_FAILED)
ea03559a
RM
515 lose (errno, "cannot map file data");
516 file_mapping = result;
517 }
518 return file_mapping + location;
519 }
520
266180eb
RM
521 const ElfW(Ehdr) *header;
522 const ElfW(Phdr) *phdr;
523 const ElfW(Phdr) *ph;
8193034b 524 size_t maplength;
b122c703 525 int type;
d66e34cd
RM
526
527 /* Look again to see if the real name matched another already loaded. */
528 for (l = _dl_loaded; l; l = l->l_next)
529 if (! strcmp (realname, l->l_name))
530 {
531 /* The object is already loaded.
532 Just bump its reference count and return it. */
266180eb 533 __close (fd);
c84142e8
UD
534
535 /* If the name is not in the list of names for this object add
536 it. */
ea03559a 537 free (realname);
0413b54c 538 add_name_to_object (l, name);
d66e34cd
RM
539 ++l->l_opencount;
540 return l;
541 }
542
8193034b
UD
543 /* Print debugging message. */
544 if (_dl_debug_files)
545 _dl_debug_message (1, "file=", name, "; generating link map\n", NULL);
546
d66e34cd
RM
547 /* Map in the first page to read the header. */
548 header = map (0, sizeof *header);
d66e34cd 549
d66e34cd 550 /* Check the header for basic validity. */
c4b72918
RM
551 if (*(Elf32_Word *) &header->e_ident !=
552#if BYTE_ORDER == LITTLE_ENDIAN
553 ((ELFMAG0 << (EI_MAG0 * 8)) |
554 (ELFMAG1 << (EI_MAG1 * 8)) |
555 (ELFMAG2 << (EI_MAG2 * 8)) |
556 (ELFMAG3 << (EI_MAG3 * 8)))
557#else
558 ((ELFMAG0 << (EI_MAG3 * 8)) |
559 (ELFMAG1 << (EI_MAG2 * 8)) |
560 (ELFMAG2 << (EI_MAG1 * 8)) |
561 (ELFMAG3 << (EI_MAG0 * 8)))
562#endif
563 )
d66e34cd 564 LOSE ("invalid ELF header");
266180eb
RM
565#define ELF32_CLASS ELFCLASS32
566#define ELF64_CLASS ELFCLASS64
567 if (header->e_ident[EI_CLASS] != ELFW(CLASS))
14e9dd67 568 LOSE ("ELF file class not " STRING(__ELF_NATIVE_CLASS) "-bit");
d66e34cd
RM
569 if (header->e_ident[EI_DATA] != byteorder)
570 LOSE ("ELF file data encoding not " byteorder_name);
571 if (header->e_ident[EI_VERSION] != EV_CURRENT)
572 LOSE ("ELF file version ident not " STRING(EV_CURRENT));
573 if (header->e_version != EV_CURRENT)
574 LOSE ("ELF file version not " STRING(EV_CURRENT));
575 if (! elf_machine_matches_host (header->e_machine))
576 LOSE ("ELF file machine architecture not " ELF_MACHINE_NAME);
266180eb 577 if (header->e_phentsize != sizeof (ElfW(Phdr)))
d66e34cd
RM
578 LOSE ("ELF file's phentsize not the expected size");
579
2064087b
RM
580#ifndef MAP_ANON
581#define MAP_ANON 0
d66e34cd
RM
582 if (_dl_zerofd == -1)
583 {
584 _dl_zerofd = _dl_sysdep_open_zero_fill ();
585 if (_dl_zerofd == -1)
ba79d61b
RM
586 {
587 __close (fd);
588 _dl_signal_error (errno, NULL, "cannot open zero fill device");
589 }
d66e34cd 590 }
2064087b 591#endif
d66e34cd 592
ba79d61b
RM
593 /* Enter the new object in the list of loaded objects. */
594 l = _dl_new_object (realname, name, l_type);
595 if (! l)
596 lose (ENOMEM, "cannot create shared object descriptor");
597 l->l_opencount = 1;
598 l->l_loader = loader;
599
b122c703
RM
600 /* Extract the remaining details we need from the ELF header
601 and then map in the program header table. */
602 l->l_entry = header->e_entry;
603 type = header->e_type;
604 l->l_phnum = header->e_phnum;
266180eb 605 phdr = map (header->e_phoff, l->l_phnum * sizeof (ElfW(Phdr)));
879bf2e6 606
b122c703
RM
607 {
608 /* Scan the program header table, collecting its load commands. */
609 struct loadcmd
610 {
266180eb 611 ElfW(Addr) mapstart, mapend, dataend, allocend;
b122c703
RM
612 off_t mapoff;
613 int prot;
614 } loadcmds[l->l_phnum], *c;
615 size_t nloadcmds = 0;
d66e34cd 616
d66e34cd 617 l->l_ld = 0;
b122c703
RM
618 l->l_phdr = 0;
619 l->l_addr = 0;
d66e34cd
RM
620 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
621 switch (ph->p_type)
622 {
623 /* These entries tell us where to find things once the file's
624 segments are mapped in. We record the addresses it says
625 verbatim, and later correct for the run-time load address. */
626 case PT_DYNAMIC:
627 l->l_ld = (void *) ph->p_vaddr;
628 break;
629 case PT_PHDR:
630 l->l_phdr = (void *) ph->p_vaddr;
631 break;
632
633 case PT_LOAD:
b122c703
RM
634 /* A load command tells us to map in part of the file.
635 We record the load commands and process them all later. */
266180eb 636 if (ph->p_align % _dl_pagesize != 0)
d66e34cd
RM
637 LOSE ("ELF load command alignment not page-aligned");
638 if ((ph->p_vaddr - ph->p_offset) % ph->p_align)
639 LOSE ("ELF load command address/offset not properly aligned");
640 {
b122c703
RM
641 struct loadcmd *c = &loadcmds[nloadcmds++];
642 c->mapstart = ph->p_vaddr & ~(ph->p_align - 1);
266180eb
RM
643 c->mapend = ((ph->p_vaddr + ph->p_filesz + _dl_pagesize - 1)
644 & ~(_dl_pagesize - 1));
b122c703
RM
645 c->dataend = ph->p_vaddr + ph->p_filesz;
646 c->allocend = ph->p_vaddr + ph->p_memsz;
647 c->mapoff = ph->p_offset & ~(ph->p_align - 1);
648 c->prot = 0;
d66e34cd 649 if (ph->p_flags & PF_R)
b122c703 650 c->prot |= PROT_READ;
d66e34cd 651 if (ph->p_flags & PF_W)
b122c703 652 c->prot |= PROT_WRITE;
d66e34cd 653 if (ph->p_flags & PF_X)
b122c703
RM
654 c->prot |= PROT_EXEC;
655 break;
656 }
657 }
d66e34cd 658
b122c703 659 /* We are done reading the file's headers now. Unmap them. */
266180eb 660 __munmap (file_mapping, mapping_size);
b122c703
RM
661
662 /* Now process the load commands and map segments into memory. */
663 c = loadcmds;
664
8193034b
UD
665 /* Length of the sections to be loaded. */
666 maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
667
b122c703
RM
668 if (type == ET_DYN || type == ET_REL)
669 {
670 /* This is a position-independent shared object. We can let the
671 kernel map it anywhere it likes, but we must have space for all
672 the segments in their specified positions relative to the first.
673 So we map the first segment without MAP_FIXED, but with its
22930c9b
RM
674 extent increased to cover all the segments. Then we remove
675 access from excess portion, and there is known sufficient space
4cca6b86
UD
676 there to remap from the later segments.
677
678 As a refinement, sometimes we have an address that we would
679 prefer to map such objects at; but this is only a preference,
680 the OS can do whatever it likes. */
b122c703 681 caddr_t mapat;
4cca6b86 682 ElfW(Addr) mappref;
f21acc89
UD
683 mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart)
684 - MAP_BASE_ADDR (l));
4cca6b86 685 mapat = map_segment (mappref, maplength, c->prot, 0, c->mapoff);
266180eb 686 l->l_addr = (ElfW(Addr)) mapat - c->mapstart;
b122c703 687
22930c9b
RM
688 /* Change protection on the excess portion to disallow all access;
689 the portions we do not remap later will be inaccessible as if
690 unallocated. Then jump into the normal segment-mapping loop to
691 handle the portion of the segment past the end of the file
692 mapping. */
0d3726c3 693 __mprotect ((caddr_t) (l->l_addr + c->mapend),
266180eb
RM
694 loadcmds[nloadcmds - 1].allocend - c->mapend,
695 0);
b122c703
RM
696 goto postmap;
697 }
4cca6b86
UD
698 else
699 {
700 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
701 fixed. */
702 ELF_FIXED_ADDRESS (loader, c->mapstart);
703 }
b122c703
RM
704
705 while (c < &loadcmds[nloadcmds])
706 {
707 if (c->mapend > c->mapstart)
708 /* Map the segment contents from the file. */
709 map_segment (l->l_addr + c->mapstart, c->mapend - c->mapstart,
710 c->prot, MAP_FIXED, c->mapoff);
711
712 postmap:
713 if (c->allocend > c->dataend)
714 {
715 /* Extra zero pages should appear at the end of this segment,
716 after the data mapped from the file. */
266180eb 717 ElfW(Addr) zero, zeroend, zeropage;
b122c703
RM
718
719 zero = l->l_addr + c->dataend;
720 zeroend = l->l_addr + c->allocend;
266180eb 721 zeropage = (zero + _dl_pagesize - 1) & ~(_dl_pagesize - 1);
d66e34cd 722
b122c703
RM
723 if (zeroend < zeropage)
724 /* All the extra data is in the last page of the segment.
725 We can just zero it. */
726 zeropage = zeroend;
727
728 if (zeropage > zero)
d66e34cd 729 {
b122c703
RM
730 /* Zero the final part of the last page of the segment. */
731 if ((c->prot & PROT_WRITE) == 0)
d66e34cd 732 {
b122c703 733 /* Dag nab it. */
266180eb
RM
734 if (__mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
735 _dl_pagesize, c->prot|PROT_WRITE) < 0)
b122c703 736 lose (errno, "cannot change memory protections");
d66e34cd 737 }
b122c703
RM
738 memset ((void *) zero, 0, zeropage - zero);
739 if ((c->prot & PROT_WRITE) == 0)
266180eb
RM
740 __mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
741 _dl_pagesize, c->prot);
b122c703 742 }
d66e34cd 743
b122c703
RM
744 if (zeroend > zeropage)
745 {
746 /* Map the remaining zero pages in from the zero fill FD. */
747 caddr_t mapat;
266180eb
RM
748 mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage,
749 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
2064087b 750 ANONFD, 0);
0413b54c 751 if (mapat == MAP_FAILED)
9b8a44cd 752 lose (errno, "cannot map zero-fill pages");
d66e34cd
RM
753 }
754 }
d66e34cd 755
b122c703 756 ++c;
879bf2e6 757 }
0d3726c3
RM
758
759 if (l->l_phdr == 0)
760 {
761 /* There was no PT_PHDR specified. We need to find the phdr in the
762 load image ourselves. We assume it is in fact in the load image
763 somewhere, and that the first load command starts at the
764 beginning of the file and thus contains the ELF file header. */
765 ElfW(Addr) bof = l->l_addr + loadcmds[0].mapstart;
766 assert (loadcmds[0].mapoff == 0);
767 l->l_phdr = (void *) (bof + ((const ElfW(Ehdr) *) bof)->e_phoff);
768 }
769 else
770 /* Adjust the PT_PHDR value by the runtime load address. */
771 (ElfW(Addr)) l->l_phdr += l->l_addr;
b122c703 772 }
d66e34cd 773
6d9756c9
RM
774 /* We are done mapping in the file. We no longer need the descriptor. */
775 __close (fd);
776
ba79d61b
RM
777 if (l->l_type == lt_library && type == ET_EXEC)
778 l->l_type = lt_executable;
9b8a44cd 779
b122c703
RM
780 if (l->l_ld == 0)
781 {
782 if (type == ET_DYN)
783 LOSE ("object file has no dynamic section");
784 }
785 else
266180eb 786 (ElfW(Addr)) l->l_ld += l->l_addr;
879bf2e6 787
463e148b
RM
788 l->l_entry += l->l_addr;
789
8193034b
UD
790 if (_dl_debug_files)
791 {
792 const size_t nibbles = sizeof (void *) * 2;
793 char buf1[nibbles + 1];
794 char buf2[nibbles + 1];
795 char buf3[nibbles + 1];
796
797 buf1[nibbles] = '\0';
798 buf2[nibbles] = '\0';
799 buf3[nibbles] = '\0';
800
801 memset (buf1, '0', nibbles);
802 memset (buf2, '0', nibbles);
803 memset (buf3, '0', nibbles);
804 _itoa_word ((unsigned long int) l->l_ld, &buf1[nibbles], 16, 0);
805 _itoa_word ((unsigned long int) l->l_addr, &buf2[nibbles], 16, 0);
806 _itoa_word (maplength, &buf3[nibbles], 16, 0);
807
808 _dl_debug_message (1, " dynamic: 0x", buf1, " base: 0x", buf2,
809 " size: 0x", buf3, "\n", NULL);
810 memset (buf1, '0', nibbles);
811 memset (buf2, '0', nibbles);
812 memset (buf3, ' ', nibbles);
813 _itoa_word ((unsigned long int) l->l_entry, &buf1[nibbles], 16, 0);
814 _itoa_word ((unsigned long int) l->l_phdr, &buf2[nibbles], 16, 0);
815 _itoa_word (l->l_phnum, &buf3[nibbles], 10, 0);
816 _dl_debug_message (1, " entry: 0x", buf1, " phdr: 0x", buf2,
817 " phnum: ", buf3, "\n\n", NULL);
818 }
819
d66e34cd
RM
820 elf_get_dynamic_info (l->l_ld, l->l_info);
821 if (l->l_info[DT_HASH])
822 _dl_setup_hash (l);
823
824 return l;
825}
ba79d61b 826\f
b5efde2f
UD
827/* Print search path. */
828static void
829print_search_path (struct r_search_path_elem **list,
830 const char *what, const char *name)
831{
12264bd7
UD
832 char buf[max_dirnamelen + max_capstrlen];
833 char *endp;
b5efde2f
UD
834 int first = 1;
835
8193034b 836 _dl_debug_message (1, " search path=", NULL);
b5efde2f
UD
837
838 while (*list != NULL && (*list)->what == what) /* Yes, ==. */
839 {
12264bd7
UD
840 char *endp = __mempcpy (buf, (*list)->dirname, (*list)->dirnamelen);
841 size_t cnt;
842
843 for (cnt = 0; cnt < ncapstr; ++cnt)
844 if ((*list)->status[cnt] != nonexisting)
845 {
846 char *cp = __mempcpy (endp, capstr[cnt].str, capstr[cnt].len);
847 cp[-1] = '\0';
848 _dl_debug_message (0, first ? "" : ":", buf, NULL);
849 first = 0;
850 }
b5efde2f 851
b5efde2f
UD
852 ++list;
853 }
854
855 if (name != NULL)
8193034b 856 _dl_debug_message (0, "\t\t(", what, " from file ",
b5efde2f
UD
857 name[0] ? name : _dl_argv[0], ")\n", NULL);
858 else
8193034b 859 _dl_debug_message (0, "\t\t(", what, ")\n", NULL);
b5efde2f
UD
860}
861\f
0a54e401 862/* Try to open NAME in one of the directories in DIRS.
ba79d61b
RM
863 Return the fd, or -1. If successful, fill in *REALNAME
864 with the malloc'd full directory name. */
865
866static int
c6222ab9 867open_path (const char *name, size_t namelen, int preloaded,
0a54e401
UD
868 struct r_search_path_elem **dirs,
869 char **realname)
ba79d61b
RM
870{
871 char *buf;
0a54e401 872 int fd = -1;
b5efde2f 873 const char *current_what = NULL;
ba79d61b 874
0a54e401 875 if (dirs == NULL || *dirs == NULL)
ba79d61b 876 {
c4029823 877 __set_errno (ENOENT);
ba79d61b
RM
878 return -1;
879 }
880
12264bd7 881 buf = __alloca (max_dirnamelen + max_capstrlen + namelen + 1);
ba79d61b
RM
882 do
883 {
0a54e401
UD
884 struct r_search_path_elem *this_dir = *dirs;
885 size_t buflen = 0;
12264bd7 886 size_t cnt;
ba79d61b 887
b5efde2f
UD
888 /* If we are debugging the search for libraries print the path
889 now if it hasn't happened now. */
890 if (_dl_debug_libs && current_what != this_dir->what)
891 {
892 current_what = this_dir->what;
893 print_search_path (dirs, current_what, this_dir->where);
894 }
895
12264bd7 896 for (cnt = 0; fd == -1 && cnt < ncapstr; ++cnt)
fd26970f 897 {
12264bd7
UD
898 /* Skip this directory if we know it does not exist. */
899 if (this_dir->status[cnt] == nonexisting)
900 continue;
0a54e401 901
12264bd7
UD
902 buflen =
903 ((char *) __mempcpy (__mempcpy (__mempcpy (buf, this_dir->dirname,
904 this_dir->dirnamelen),
905 capstr[cnt].str, capstr[cnt].len),
906 name, namelen)
907 - buf);
908
909 /* Print name we try if this is wanted. */
b5efde2f 910 if (_dl_debug_libs)
8193034b 911 _dl_debug_message (1, " trying file=", buf, "\n", NULL);
b5efde2f 912
0a54e401 913 fd = __open (buf, O_RDONLY);
12264bd7 914 if (this_dir->status[cnt] == unknown)
0a54e401 915 if (fd != -1)
12264bd7 916 this_dir->status[cnt] = existing;
0a54e401
UD
917 else
918 {
919 /* We failed to open machine dependent library. Let's
920 test whether there is any directory at all. */
921 struct stat st;
fd26970f 922
12264bd7 923 buf[this_dir->dirnamelen + capstr[cnt].len] = '\0';
fd26970f 924
0a614877
UD
925 if (__xstat (_STAT_VER, buf, &st) != 0
926 || ! S_ISDIR (st.st_mode))
0a54e401 927 /* The directory does not exist ot it is no directory. */
12264bd7 928 this_dir->status[cnt] = nonexisting;
fd26970f 929 else
12264bd7 930 this_dir->status[cnt] = existing;
0a54e401 931 }
fd26970f 932
c6222ab9
UD
933 if (fd != -1 && preloaded && __libc_enable_secure)
934 {
935 /* This is an extra security effort to make sure nobody can
936 preload broken shared objects which are in the trusted
937 directories and so exploit the bugs. */
938 struct stat st;
939
940 if (__fxstat (_STAT_VER, fd, &st) != 0
941 || (st.st_mode & S_ISUID) == 0)
942 {
943 /* The shared object cannot be tested for being SUID
944 or this bit is not set. In this case we must not
945 use this object. */
946 __close (fd);
947 fd = -1;
948 /* We simply ignore the file, signal this by setting
949 the error value which would have been set by `open'. */
950 errno = ENOENT;
951 }
952 }
ba79d61b
RM
953 }
954
ba79d61b
RM
955 if (fd != -1)
956 {
957 *realname = malloc (buflen);
c6222ab9 958 if (*realname != NULL)
ba79d61b
RM
959 {
960 memcpy (*realname, buf, buflen);
961 return fd;
962 }
963 else
964 {
965 /* No memory for the name, we certainly won't be able
966 to load and link it. */
967 __close (fd);
968 return -1;
969 }
970 }
971 if (errno != ENOENT && errno != EACCES)
972 /* The file exists and is readable, but something went wrong. */
973 return -1;
974 }
0a54e401 975 while (*++dirs != NULL);
ba79d61b
RM
976
977 return -1;
978}
979
980/* Map in the shared object file NAME. */
981
982struct link_map *
d0fc4041 983internal_function
c6222ab9
UD
984_dl_map_object (struct link_map *loader, const char *name, int preloaded,
985 int type, int trace_mode)
ba79d61b
RM
986{
987 int fd;
988 char *realname;
14bab8de 989 char *name_copy;
ba79d61b
RM
990 struct link_map *l;
991
992 /* Look for this name among those already loaded. */
993 for (l = _dl_loaded; l; l = l->l_next)
f41c8091
UD
994 {
995 /* If the requested name matches the soname of a loaded object,
996 use that object. Elide this check for names that have not
997 yet been opened. */
998 if (l->l_opencount <= 0)
999 continue;
1000 if (!_dl_name_match_p (name, l))
1001 {
1002 const char *soname;
1003
1004 if (l->l_info[DT_SONAME] == NULL)
1005 continue;
1006
1007 soname = (const char *) (l->l_addr
1008 + l->l_info[DT_STRTAB]->d_un.d_ptr
1009 + l->l_info[DT_SONAME]->d_un.d_val);
1010 if (strcmp (name, soname) != 0)
1011 continue;
1012
1013 /* We have a match on a new name -- cache it. */
1014 add_name_to_object (l, local_strdup (soname));
1015 }
1016
1017 /* We have a match -- bump the reference count and return it. */
1018 ++l->l_opencount;
1019 return l;
1020 }
ba79d61b 1021
8193034b
UD
1022 /* Display information if we are debugging. */
1023 if (_dl_debug_files && loader != NULL)
1024 _dl_debug_message (1, "\nfile=", name, "; needed by ",
1025 loader->l_name[0] ? loader->l_name : _dl_argv[0],
1026 "\n", NULL);
1027
ba79d61b
RM
1028 if (strchr (name, '/') == NULL)
1029 {
1030 /* Search for NAME in several places. */
1031
1032 size_t namelen = strlen (name) + 1;
1033
b5efde2f 1034 if (_dl_debug_libs)
8193034b 1035 _dl_debug_message (1, "find library=", name, "; searching\n", NULL);
b5efde2f 1036
ba79d61b 1037 fd = -1;
a23db8e4
RM
1038
1039 /* First try the DT_RPATH of the dependent object that caused NAME
1040 to be loaded. Then that object's dependent, and on up. */
1041 for (l = loader; fd == -1 && l; l = l->l_loader)
ba79d61b 1042 if (l && l->l_info[DT_RPATH])
fd26970f 1043 {
0a54e401
UD
1044 /* Make sure the cache information is available. */
1045 if (l->l_rpath_dirs == NULL)
1046 {
1047 size_t ptrval = (l->l_addr
1048 + l->l_info[DT_STRTAB]->d_un.d_ptr
1049 + l->l_info[DT_RPATH]->d_un.d_val);
1050 l->l_rpath_dirs =
b5efde2f
UD
1051 decompose_rpath ((const char *) ptrval, 0,
1052 "RPATH", l->l_name);
0a54e401
UD
1053 }
1054
1055 if (l->l_rpath_dirs != (struct r_search_path_elem **) -1l)
c6222ab9
UD
1056 fd = open_path (name, namelen, preloaded, l->l_rpath_dirs,
1057 &realname);
0a54e401
UD
1058 }
1059
1060 /* If dynamically linked, try the DT_RPATH of the executable itself
1061 and the LD_LIBRARY_PATH environment variable. */
1062 l = _dl_loaded;
1063 if (fd == -1 && l && l->l_type != lt_loaded
1064 && l->l_rpath_dirs != (struct r_search_path_elem **) -1l)
c6222ab9 1065 fd = open_path (name, namelen, preloaded, l->l_rpath_dirs, &realname);
fd26970f 1066
40a55d20
UD
1067 /* This is used if a static binary uses dynamic loading and there
1068 is a LD_LIBRARY_PATH given. */
1069 if (fd == -1 && fake_path_list != NULL)
c6222ab9 1070 fd = open_path (name, namelen, preloaded, fake_path_list, &realname);
40a55d20 1071
f18edac3
RM
1072 if (fd == -1)
1073 {
1074 /* Check the list of libraries in the file /etc/ld.so.cache,
1075 for compatibility with Linux's ldconfig program. */
1076 extern const char *_dl_load_cache_lookup (const char *name);
1077 const char *cached = _dl_load_cache_lookup (name);
1078 if (cached)
1079 {
1080 fd = __open (cached, O_RDONLY);
1081 if (fd != -1)
1082 {
706074a5
UD
1083 realname = local_strdup (cached);
1084 if (realname == NULL)
f18edac3
RM
1085 {
1086 __close (fd);
1087 fd = -1;
1088 }
1089 }
1090 }
1091 }
0a54e401 1092
a23db8e4 1093 /* Finally, try the default path. */
ba79d61b 1094 if (fd == -1)
c6222ab9 1095 fd = open_path (name, namelen, preloaded, rtld_search_dirs, &realname);
b5efde2f
UD
1096
1097 /* Add another newline when we a tracing the library loading. */
1098 if (_dl_debug_libs)
8193034b 1099 _dl_debug_message (1, "\n", NULL);
ba79d61b
RM
1100 }
1101 else
1102 {
1103 fd = __open (name, O_RDONLY);
1104 if (fd != -1)
1105 {
706074a5
UD
1106 realname = local_strdup (name);
1107 if (realname == NULL)
ba79d61b
RM
1108 {
1109 __close (fd);
1110 fd = -1;
1111 }
1112 }
1113 }
1114
706074a5
UD
1115 if (fd != -1)
1116 {
14bab8de
UD
1117 name_copy = local_strdup (name);
1118 if (name_copy == NULL)
706074a5
UD
1119 {
1120 __close (fd);
1121 fd = -1;
1122 }
1123 }
1124
ba79d61b 1125 if (fd == -1)
46ec036d
UD
1126 {
1127 if (trace_mode)
1128 {
1129 /* We haven't found an appropriate library. But since we
1130 are only interested in the list of libraries this isn't
1131 so severe. Fake an entry with all the information we
1ef32c3d 1132 have. */
fd26970f 1133 static const ElfW(Symndx) dummy_bucket = STN_UNDEF;
46ec036d
UD
1134
1135 /* Enter the new object in the list of loaded objects. */
1136 if ((name_copy = local_strdup (name)) == NULL
1137 || (l = _dl_new_object (name_copy, name, type)) == NULL)
1138 _dl_signal_error (ENOMEM, name,
1139 "cannot create shared object descriptor");
1140 /* We use an opencount of 0 as a sign for the faked entry. */
1141 l->l_opencount = 0;
1142 l->l_reserved = 0;
fd26970f
UD
1143 l->l_buckets = &dummy_bucket;
1144 l->l_nbuckets = 1;
1145 l->l_relocated = 1;
1146
1147 return l;
46ec036d
UD
1148 }
1149 else
1150 _dl_signal_error (errno, name, "cannot open shared object file");
1151 }
ba79d61b 1152
14bab8de 1153 return _dl_map_object_from_fd (name_copy, fd, realname, loader, type);
ba79d61b 1154}