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