1 /* vi: set sw=4 ts=4: */
3 * This file contains the helper routines to load an ELF shared
4 * library into memory and add the symbol table info to the chain.
6 * Copyright (C) 2000-2004 by Erik Andersen <andersen@codepoet.org>
7 * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
8 * David Engel, Hongjiu Lu and Mitch D'Souza
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. The name of the above contributors may not be
16 * used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #ifdef __LDSO_CACHE_SUPPORT__
37 static caddr_t _dl_cache_addr = NULL;
38 static size_t _dl_cache_size = 0;
40 int _dl_map_cache(void)
48 if (_dl_cache_addr == (caddr_t) - 1)
50 else if (_dl_cache_addr != NULL)
53 if (_dl_stat(LDSO_CACHE, &st)
54 || (fd = _dl_open(LDSO_CACHE, O_RDONLY, 0)) < 0) {
55 _dl_cache_addr = (caddr_t) - 1; /* so we won't try again */
59 _dl_cache_size = st.st_size;
60 _dl_cache_addr = (caddr_t) _dl_mmap(0, _dl_cache_size, PROT_READ, MAP_SHARED, fd, 0);
62 if (_dl_mmap_check_error(_dl_cache_addr)) {
63 _dl_dprintf(2, "%s: can't map cache '%s'\n",
64 _dl_progname, LDSO_CACHE);
68 header = (header_t *) _dl_cache_addr;
70 if (_dl_cache_size < sizeof(header_t) ||
71 _dl_memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
72 || _dl_memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
74 (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
75 || _dl_cache_addr[_dl_cache_size - 1] != '\0')
77 _dl_dprintf(2, "%s: cache '%s' is corrupt\n", _dl_progname,
82 strtabsize = _dl_cache_size - sizeof(header_t) -
83 header->nlibs * sizeof(libentry_t);
84 libent = (libentry_t *) & header[1];
86 for (i = 0; i < header->nlibs; i++) {
87 if (libent[i].sooffset >= strtabsize ||
88 libent[i].liboffset >= strtabsize)
90 _dl_dprintf(2, "%s: cache '%s' is corrupt\n", _dl_progname, LDSO_CACHE);
98 _dl_munmap(_dl_cache_addr, _dl_cache_size);
99 _dl_cache_addr = (caddr_t) - 1;
103 int _dl_unmap_cache(void)
105 if (_dl_cache_addr == NULL || _dl_cache_addr == (caddr_t) - 1)
109 _dl_munmap(_dl_cache_addr, _dl_cache_size);
110 _dl_cache_addr = NULL;
119 _dl_protect_relro (struct elf_resolve *l)
121 ElfW(Addr) start = ((l->loadaddr + l->relro_addr)
122 & ~(_dl_pagesize - 1));
123 ElfW(Addr) end = ((l->loadaddr + l->relro_addr + l->relro_size)
124 & ~(_dl_pagesize - 1));
125 #if defined (__SUPPORT_LD_DEBUG__)
127 _dl_dprintf(2, "RELRO protecting %s: start:%x, end:%x\n", l->libname, start, end);
130 _dl_mprotect ((void *) start, end - start, PROT_READ) < 0) {
131 _dl_dprintf(2, "%s: cannot apply additional memory protection after relocation", l->libname);
136 /* This function's behavior must exactly match that
137 * in uClibc/ldso/util/ldd.c */
138 static struct elf_resolve *
139 search_for_named_library(const char *name, int secure, const char *path_list,
140 struct dyn_elf **rpnt)
143 char mylibname[2050];
144 struct elf_resolve *tpnt;
150 /* We need a writable copy of this string */
151 path = _dl_strdup(path_list);
153 _dl_dprintf(2, "Out of memory!\n");
157 /* Unlike ldd.c, don't bother to eliminate double //s */
159 /* Replace colons with zeros in path_list */
160 /* : at the beginning or end of path maps to CWD */
161 /* :: anywhere maps CWD */
172 _dl_strcpy(mylibname, path_n);
174 _dl_strcpy(mylibname, "."); /* Assume current dir if empty path */
175 _dl_strcat(mylibname, "/");
176 _dl_strcat(mylibname, name);
177 if ((tpnt = _dl_load_elf_shared_library(secure, rpnt, mylibname)) != NULL)
186 /* Check if the named library is already loaded... */
187 struct elf_resolve *_dl_check_if_named_library_is_loaded(const char *full_libname,
188 int trace_loaded_objects)
190 const char *pnt, *pnt1;
191 struct elf_resolve *tpnt1;
192 const char *libname, *libname2;
193 static const char libc[] = "libc.so.";
194 static const char aborted_wrong_lib[] = "%s: aborted attempt to load %s!\n";
196 pnt = libname = full_libname;
198 _dl_if_debug_dprint("Checking if '%s' is already loaded\n", full_libname);
199 /* quick hack to ensure mylibname buffer doesn't overflow. don't
200 allow full_libname or any directory to be longer than 1024. */
201 if (_dl_strlen(full_libname) > 1024)
204 /* Skip over any initial initial './' and '/' stuff to
205 * get the short form libname with no path garbage */
206 pnt1 = _dl_strrchr(pnt, '/');
211 /* Make sure they are not trying to load the wrong C library!
212 * This sometimes happens esp with shared libraries when the
213 * library path is somehow wrong! */
214 #define isdigit(c) (c >= '0' && c <= '9')
215 if ((_dl_strncmp(libname, libc, 8) == 0) && _dl_strlen(libname) >=8 &&
218 /* Abort attempts to load glibc, libc5, etc */
219 if ( libname[8]!='0') {
220 if (!trace_loaded_objects) {
221 _dl_dprintf(2, aborted_wrong_lib, libname, _dl_progname);
228 /* Critical step! Weed out duplicates early to avoid
229 * function aliasing, which wastes memory, and causes
230 * really bad things to happen with weaks and globals. */
231 for (tpnt1 = _dl_loaded_modules; tpnt1; tpnt1 = tpnt1->next) {
233 /* Skip over any initial initial './' and '/' stuff to
234 * get the short form libname with no path garbage */
235 libname2 = tpnt1->libname;
236 pnt1 = _dl_strrchr(libname2, '/');
241 if (_dl_strcmp(libname2, libname) == 0) {
242 /* Well, that was certainly easy */
251 /* Used to return error codes back to dlopen et. al. */
252 unsigned long _dl_error_number;
253 unsigned long _dl_internal_error_number;
255 struct elf_resolve *_dl_load_shared_library(int secure, struct dyn_elf **rpnt,
256 struct elf_resolve *tpnt, char *full_libname, int __attribute__((unused)) trace_loaded_objects)
259 struct elf_resolve *tpnt1;
262 _dl_internal_error_number = 0;
263 libname = full_libname;
265 /* quick hack to ensure mylibname buffer doesn't overflow. don't
266 allow full_libname or any directory to be longer than 1024. */
267 if (_dl_strlen(full_libname) > 1024)
270 /* Skip over any initial initial './' and '/' stuff to
271 * get the short form libname with no path garbage */
272 pnt1 = _dl_strrchr(libname, '/');
277 /* Critical step! Weed out duplicates early to avoid
278 * function aliasing, which wastes memory, and causes
279 * really bad things to happen with weaks and globals. */
280 if ((tpnt1=_dl_check_if_named_library_is_loaded(libname, trace_loaded_objects))!=NULL) {
281 tpnt1->usage_count++;
285 _dl_if_debug_dprint("\tfind library='%s'; searching\n", libname);
286 /* If the filename has any '/', try it straight and leave it at that.
287 For IBCS2 compatibility under linux, we substitute the string
288 /usr/i486-sysv4/lib for /usr/lib in library names. */
290 if (libname != full_libname) {
291 _dl_if_debug_dprint("\ttrying file='%s'\n", full_libname);
292 tpnt1 = _dl_load_elf_shared_library(secure, rpnt, full_libname);
300 * The ABI specifies that RPATH is searched before LD_LIBRARY_PATH or
301 * the default path of /usr/lib. Check in rpath directories.
303 pnt = (tpnt ? (char *) tpnt->dynamic_info[DT_RPATH] : NULL);
305 pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
306 _dl_if_debug_dprint("\tsearching RPATH='%s'\n", pnt);
307 if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
311 /* Check in LD_{ELF_}LIBRARY_PATH, if specified and allowed */
312 if (_dl_library_path) {
313 _dl_if_debug_dprint("\tsearching LD_LIBRARY_PATH='%s'\n", _dl_library_path);
314 if ((tpnt1 = search_for_named_library(libname, secure, _dl_library_path, rpnt)) != NULL)
320 * The ABI specifies that RUNPATH is searched after LD_LIBRARY_PATH.
322 pnt = (tpnt ? (char *)tpnt->dynamic_info[DT_RUNPATH] : NULL);
324 pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
325 _dl_if_debug_dprint("\tsearching RUNPATH='%s'\n", pnt);
326 if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
331 * Where should the cache be searched? There is no such concept in the
332 * ABI, so we have some flexibility here. For now, search it before
333 * the hard coded paths that follow (i.e before /lib and /usr/lib).
335 #ifdef __LDSO_CACHE_SUPPORT__
336 if (_dl_cache_addr != NULL && _dl_cache_addr != (caddr_t) - 1) {
338 header_t *header = (header_t *) _dl_cache_addr;
339 libentry_t *libent = (libentry_t *) & header[1];
340 char *strs = (char *) &libent[header->nlibs];
342 _dl_if_debug_dprint("\tsearching cache='%s'\n", LDSO_CACHE);
343 for (i = 0; i < header->nlibs; i++) {
344 if ((libent[i].flags == LIB_ELF ||
345 libent[i].flags == LIB_ELF_LIBC0 ||
346 libent[i].flags == LIB_ELF_LIBC5) &&
347 _dl_strcmp(libname, strs + libent[i].sooffset) == 0 &&
348 (tpnt1 = _dl_load_elf_shared_library(secure,
349 rpnt, strs + libent[i].liboffset)))
355 /* Look for libraries wherever the shared library loader
357 _dl_if_debug_dprint("\tsearching ldso dir='%s'\n", _dl_ldsopath);
358 if ((tpnt1 = search_for_named_library(libname, secure, _dl_ldsopath, rpnt)) != NULL)
364 /* Lastly, search the standard list of paths for the library.
365 This list must exactly match the list in uClibc/ldso/util/ldd.c */
366 _dl_if_debug_dprint("\tsearching full lib path list\n");
367 if ((tpnt1 = search_for_named_library(libname, secure,
368 UCLIBC_RUNTIME_PREFIX "lib:"
369 UCLIBC_RUNTIME_PREFIX "usr/lib"
370 #ifndef __LDSO_CACHE_SUPPORT__
371 ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
380 /* Well, we shot our wad on that one. All we can do now is punt */
381 if (_dl_internal_error_number)
382 _dl_error_number = _dl_internal_error_number;
384 _dl_error_number = LD_ERROR_NOFILE;
385 #if defined (__SUPPORT_LD_DEBUG__)
386 if(_dl_debug) _dl_dprintf(2, "Bummer: could not find '%s'!\n", libname);
393 * Read one ELF library into memory, mmap it into the correct locations and
394 * add the symbol info to the symbol chain. Perform any relocations that
398 struct elf_resolve *_dl_load_elf_shared_library(int secure,
399 struct dyn_elf **rpnt, char *libname)
402 unsigned long dynamic_addr = 0;
404 struct elf_resolve *tpnt;
406 char *status, *header;
407 unsigned long dynamic_info[DYNAMIC_SIZE];
409 unsigned long libaddr;
410 unsigned long minvma = 0xffffffff, maxvma = 0;
411 int i, flags, piclib, infile;
412 ElfW(Addr) relro_addr = 0;
413 size_t relro_size = 0;
415 /* If this file is already loaded, skip this step */
416 tpnt = _dl_check_hashed_files(libname);
419 (*rpnt)->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
420 _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
421 (*rpnt)->next->prev = (*rpnt);
422 *rpnt = (*rpnt)->next;
424 tpnt->symbol_scope = _dl_symbol_tables;
427 tpnt->libtype = elf_lib;
428 #if defined (__SUPPORT_LD_DEBUG__)
429 if(_dl_debug) _dl_dprintf(2, "file='%s'; already loaded\n", libname);
434 /* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
435 we don't load the library if it isn't setuid. */
440 if (_dl_stat(libname, &st) || !(st.st_mode & S_ISUID))
445 infile = _dl_open(libname, O_RDONLY, 0);
449 * NO! When we open shared libraries we may search several paths.
450 * it is inappropriate to generate an error here.
452 _dl_dprintf(2, "%s: can't open '%s'\n", _dl_progname, libname);
454 _dl_internal_error_number = LD_ERROR_NOFILE;
458 header = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
459 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
460 if (_dl_mmap_check_error(header)) {
461 _dl_dprintf(2, "%s: can't map '%s'\n", _dl_progname, libname);
462 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
467 _dl_read(infile, header, _dl_pagesize);
468 epnt = (ElfW(Ehdr) *) (intptr_t) header;
469 if (epnt->e_ident[0] != 0x7f ||
470 epnt->e_ident[1] != 'E' ||
471 epnt->e_ident[2] != 'L' ||
472 epnt->e_ident[3] != 'F')
474 _dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
476 _dl_internal_error_number = LD_ERROR_NOTELF;
478 _dl_munmap(header, _dl_pagesize);
482 if ((epnt->e_type != ET_DYN) || (epnt->e_machine != MAGIC1
484 && epnt->e_machine != MAGIC2
488 _dl_internal_error_number =
489 (epnt->e_type != ET_DYN ? LD_ERROR_NOTDYN : LD_ERROR_NOTMAGIC);
490 _dl_dprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
491 "\n", _dl_progname, libname);
493 _dl_munmap(header, _dl_pagesize);
497 ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
500 for (i = 0; i < epnt->e_phnum; i++) {
502 if (ppnt->p_type == PT_DYNAMIC) {
504 _dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
505 _dl_progname, libname);
506 dynamic_addr = ppnt->p_vaddr;
509 if (ppnt->p_type == PT_LOAD) {
510 /* See if this is a PIC library. */
511 if (i == 0 && ppnt->p_vaddr > 0x1000000) {
513 minvma = ppnt->p_vaddr;
515 if (piclib && ppnt->p_vaddr < minvma) {
516 minvma = ppnt->p_vaddr;
518 if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
519 maxvma = ppnt->p_vaddr + ppnt->p_memsz;
525 maxvma = (maxvma + ADDR_ALIGN) & ~ADDR_ALIGN;
526 minvma = minvma & ~0xffffU;
528 flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
532 status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
533 maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
534 if (_dl_mmap_check_error(status)) {
535 _dl_dprintf(2, "%s: can't map %s\n", _dl_progname, libname);
536 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
538 _dl_munmap(header, _dl_pagesize);
541 libaddr = (unsigned long) status;
544 /* Get the memory to store the library */
545 ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
547 for (i = 0; i < epnt->e_phnum; i++) {
548 if (ppnt->p_type == PT_GNU_RELRO) {
549 relro_addr = ppnt->p_vaddr;
550 relro_size = ppnt->p_memsz;
552 if (ppnt->p_type == PT_LOAD) {
554 /* See if this is a PIC library. */
555 if (i == 0 && ppnt->p_vaddr > 0x1000000) {
557 /* flags |= MAP_FIXED; */
562 if (ppnt->p_flags & PF_W) {
563 unsigned long map_size;
566 status = (char *) _dl_mmap((char *) ((piclib ? libaddr : 0) +
567 (ppnt->p_vaddr & PAGE_ALIGN)), (ppnt->p_vaddr & ADDR_ALIGN)
568 + ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags, infile,
569 ppnt->p_offset & OFFS_ALIGN);
571 if (_dl_mmap_check_error(status)) {
572 _dl_dprintf(2, "%s: can't map '%s'\n",
573 _dl_progname, libname);
574 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
575 _dl_munmap((char *) libaddr, maxvma - minvma);
577 _dl_munmap(header, _dl_pagesize);
581 /* Pad the last page with zeroes. */
582 cpnt = (char *) (status + (ppnt->p_vaddr & ADDR_ALIGN) +
584 while (((unsigned long) cpnt) & ADDR_ALIGN)
587 /* I am not quite sure if this is completely
588 * correct to do or not, but the basic way that
589 * we handle bss segments is that we mmap
590 * /dev/zero if there are any pages left over
591 * that are not mapped as part of the file */
593 map_size = (ppnt->p_vaddr + ppnt->p_filesz + ADDR_ALIGN) & PAGE_ALIGN;
595 if (map_size < ppnt->p_vaddr + ppnt->p_memsz)
596 status = (char *) _dl_mmap((char *) map_size +
597 (piclib ? libaddr : 0),
598 ppnt->p_vaddr + ppnt->p_memsz - map_size,
599 LXFLAGS(ppnt->p_flags), flags | MAP_ANONYMOUS, -1, 0);
601 status = (char *) _dl_mmap((char *) (ppnt->p_vaddr & PAGE_ALIGN)
602 + (piclib ? libaddr : 0), (ppnt->p_vaddr & ADDR_ALIGN) +
603 ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags,
604 infile, ppnt->p_offset & OFFS_ALIGN);
605 if (_dl_mmap_check_error(status)) {
606 _dl_dprintf(2, "%s: can't map '%s'\n", _dl_progname, libname);
607 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
608 _dl_munmap((char *) libaddr, maxvma - minvma);
610 _dl_munmap(header, _dl_pagesize);
614 /* if(libaddr == 0 && piclib) {
615 libaddr = (unsigned long) status;
623 /* For a non-PIC library, the addresses are all absolute */
625 dynamic_addr += (unsigned long) libaddr;
629 * OK, the ELF library is now loaded into VM in the correct locations
630 * The next step is to go through and do the dynamic linking (if needed).
633 /* Start by scanning the dynamic section to get all of the pointers */
636 _dl_internal_error_number = LD_ERROR_NODYNAMIC;
637 _dl_dprintf(2, "%s: '%s' is missing a dynamic section\n",
638 _dl_progname, libname);
639 _dl_munmap(header, _dl_pagesize);
643 dpnt = (Elf32_Dyn *) dynamic_addr;
644 _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
645 _dl_parse_dynamic_info(dpnt, dynamic_info, NULL, libaddr);
646 /* If the TEXTREL is set, this means that we need to make the pages
647 writable before we perform relocations. Do this now. They get set
650 if (dynamic_info[DT_TEXTREL]) {
651 #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
652 ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
653 for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
654 if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
655 _dl_mprotect((void *) ((piclib ? libaddr : 0) +
656 (ppnt->p_vaddr & PAGE_ALIGN)),
657 (ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
658 PROT_READ | PROT_WRITE | PROT_EXEC);
661 _dl_dprintf(_dl_debug_file, "Can't modify %s's text section. Use GCC option -fPIC for shared objects, please.\n",libname);
666 tpnt = _dl_add_elf_hash_table(libname, (char *) libaddr, dynamic_info,
668 tpnt->relro_addr = relro_addr;
669 tpnt->relro_size = relro_size;
670 tpnt->ppnt = (ElfW(Phdr) *)(intptr_t) (tpnt->loadaddr + epnt->e_phoff);
671 tpnt->n_phent = epnt->e_phnum;
674 * Add this object into the symbol chain
677 (*rpnt)->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
678 _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
679 (*rpnt)->next->prev = (*rpnt);
680 *rpnt = (*rpnt)->next;
682 tpnt->symbol_scope = _dl_symbol_tables;
685 tpnt->libtype = elf_lib;
688 * OK, the next thing we need to do is to insert the dynamic linker into
689 * the proper entry in the GOT so that the PLT symbols can be properly
693 lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
696 lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT]);
697 INIT_GOT(lpnt, tpnt);
700 #if defined (__SUPPORT_LD_DEBUG__)
702 _dl_dprintf(2, "\n\tfile='%s'; generating link map\n", libname);
703 _dl_dprintf(2, "\t\tdynamic: %x base: %x\n",
704 dynamic_addr, libaddr);
705 _dl_dprintf(2, "\t\t entry: %x phdr: %x phnum: %x\n\n",
706 epnt->e_entry + libaddr, tpnt->ppnt, tpnt->n_phent);
710 _dl_munmap(header, _dl_pagesize);
714 /* now_flag must be RTLD_NOW or zero */
715 int _dl_fixup(struct dyn_elf *rpnt, int now_flag)
718 struct elf_resolve *tpnt;
719 Elf32_Word reloc_size, reloc_addr, relative_count;
722 goof += _dl_fixup(rpnt->next, now_flag);
725 #if defined (__SUPPORT_LD_DEBUG__)
726 if(!(tpnt->init_flag & RELOCS_DONE))
727 _dl_if_debug_dprint("\nrelocation processing: %s\n", tpnt->libname);
730 if (unlikely(tpnt->dynamic_info[UNSUPPORTED_RELOC_TYPE])) {
731 _dl_if_debug_dprint("%s: can't handle %s relocation records\n",
732 _dl_progname, UNSUPPORTED_RELOC_STR);
737 reloc_size = tpnt->dynamic_info[DT_RELOC_TABLE_SIZE];
738 /* On some machines, notably SPARC & PPC, DT_REL* includes DT_JMPREL in its
739 range. Note that according to the ELF spec, this is completely legal! */
740 #ifdef ELF_MACHINE_PLTREL_OVERLAP
741 reloc_size -= tpnt->dynamic_info [DT_PLTRELSZ];
743 if (tpnt->dynamic_info[DT_RELOC_TABLE_ADDR] &&
744 !(tpnt->init_flag & RELOCS_DONE)) {
745 tpnt->init_flag |= RELOCS_DONE;
746 reloc_addr = tpnt->dynamic_info[DT_RELOC_TABLE_ADDR];
747 relative_count = tpnt->dynamic_info[DT_RELCONT_IDX];
748 if (relative_count) { /* Optimize the XX_RELATIVE relocations if possible */
749 reloc_size -= relative_count * sizeof(ELF_RELOC);
750 elf_machine_relative (tpnt->loadaddr, reloc_addr, relative_count);
751 reloc_addr += relative_count * sizeof(ELF_RELOC);
753 goof += _dl_parse_relocation_information(rpnt,
757 if (tpnt->dynamic_info[DT_BIND_NOW])
759 if (tpnt->dynamic_info[DT_JMPREL] &&
760 (!(tpnt->init_flag & JMP_RELOCS_DONE) ||
761 (now_flag && !(tpnt->rtld_flags & now_flag)))) {
762 tpnt->rtld_flags |= now_flag;
763 tpnt->init_flag |= JMP_RELOCS_DONE;
764 if (!(tpnt->rtld_flags & RTLD_NOW)) {
765 _dl_parse_lazy_relocation_information(rpnt,
766 tpnt->dynamic_info[DT_JMPREL],
767 tpnt->dynamic_info [DT_PLTRELSZ]);
769 goof += _dl_parse_relocation_information(rpnt,
770 tpnt->dynamic_info[DT_JMPREL],
771 tpnt->dynamic_info[DT_PLTRELSZ]);
777 /* Minimal printf which handles only %s, %d, and %x */
778 void _dl_dprintf(int fd, const char *fmt, ...)
782 char *start, *ptr, *string;
785 buf = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
786 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
787 if (_dl_mmap_check_error(buf)) {
788 _dl_write(fd, "mmap of a spare page failed!\n", 29);
797 if (_dl_strlen(fmt) >= (_dl_pagesize - 1)) {
798 _dl_write(fd, "overflow\n", 11);
802 _dl_strcpy(buf, fmt);
806 while (*ptr != '%' && *ptr) {
812 _dl_write(fd, start, _dl_strlen(start));
816 string = va_arg(args, char *);
819 _dl_write(fd, "(null)", 6);
821 _dl_write(fd, string, _dl_strlen(string));
828 num = va_arg(args, int);
830 string = _dl_simple_ltoa(tmp, num);
831 _dl_write(fd, string, _dl_strlen(string));
838 num = va_arg(args, int);
840 string = _dl_simple_ltoahex(tmp, num);
841 _dl_write(fd, string, _dl_strlen(string));
845 _dl_write(fd, "(null)", 6);
851 _dl_write(fd, start, _dl_strlen(start));
855 _dl_munmap(buf, _dl_pagesize);
859 char *_dl_strdup(const char *string)
864 len = _dl_strlen(string);
865 retval = _dl_malloc(len + 1);
866 _dl_strcpy(retval, string);
870 void _dl_parse_dynamic_info(Elf32_Dyn *dpnt, unsigned long dynamic_info[], void *debug_addr, Elf32_Addr load_off)
872 __dl_parse_dynamic_info(dpnt, dynamic_info, debug_addr, load_off);
875 #if ! defined LIBDL || (! defined PIC && ! defined __PIC__)
877 __dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info, size_t size, void *data), void *data)
879 struct elf_resolve *l;
880 struct dl_phdr_info info;
883 for (l = _dl_loaded_modules; l != NULL; l = l->next) {
884 info.dlpi_addr = l->loadaddr;
885 info.dlpi_name = l->libname;
886 info.dlpi_phdr = l->ppnt;
887 info.dlpi_phnum = l->n_phent;
888 ret = callback (&info, sizeof (struct dl_phdr_info), data);
894 strong_alias(__dl_iterate_phdr, dl_iterate_phdr);