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