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