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 #if defined (__SUPPORT_LD_DEBUG__)
36 # define _dl_if_debug_dprint(fmt, args...) \
39 _dl_dprintf(_dl_debug_file, "%s():%i: " fmt, __FUNCTION__, __LINE__, ## args); \
42 # define _dl_if_debug_dprint(fmt, args...)
45 #ifdef __LDSO_CACHE_SUPPORT__
47 static caddr_t _dl_cache_addr = NULL;
48 static size_t _dl_cache_size = 0;
50 int _dl_map_cache(void)
58 if (_dl_cache_addr == (caddr_t) - 1)
60 else if (_dl_cache_addr != NULL)
63 if (_dl_stat(LDSO_CACHE, &st)
64 || (fd = _dl_open(LDSO_CACHE, O_RDONLY, 0)) < 0) {
65 _dl_cache_addr = (caddr_t) - 1; /* so we won't try again */
69 _dl_cache_size = st.st_size;
70 _dl_cache_addr = (caddr_t) _dl_mmap(0, _dl_cache_size, PROT_READ, MAP_SHARED, fd, 0);
72 if (_dl_mmap_check_error(_dl_cache_addr)) {
73 _dl_dprintf(2, "%s: can't map cache '%s'\n",
74 _dl_progname, LDSO_CACHE);
78 header = (header_t *) _dl_cache_addr;
80 if (_dl_cache_size < sizeof(header_t) ||
81 _dl_memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
82 || _dl_memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
84 (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
85 || _dl_cache_addr[_dl_cache_size - 1] != '\0')
87 _dl_dprintf(2, "%s: cache '%s' is corrupt\n", _dl_progname,
92 strtabsize = _dl_cache_size - sizeof(header_t) -
93 header->nlibs * sizeof(libentry_t);
94 libent = (libentry_t *) & header[1];
96 for (i = 0; i < header->nlibs; i++) {
97 if (libent[i].sooffset >= strtabsize ||
98 libent[i].liboffset >= strtabsize)
100 _dl_dprintf(2, "%s: cache '%s' is corrupt\n", _dl_progname, LDSO_CACHE);
108 _dl_munmap(_dl_cache_addr, _dl_cache_size);
109 _dl_cache_addr = (caddr_t) - 1;
113 int _dl_unmap_cache(void)
115 if (_dl_cache_addr == NULL || _dl_cache_addr == (caddr_t) - 1)
119 _dl_munmap(_dl_cache_addr, _dl_cache_size);
120 _dl_cache_addr = NULL;
129 _dl_protect_relro (struct elf_resolve *l)
131 ElfW(Addr) start = ((l->loadaddr + l->relro_addr)
132 & ~(_dl_pagesize - 1));
133 ElfW(Addr) end = ((l->loadaddr + l->relro_addr + l->relro_size)
134 & ~(_dl_pagesize - 1));
135 #if defined (__SUPPORT_LD_DEBUG__)
137 _dl_dprintf(2, "RELRO protecting %s: start:%x, end:%x\n", l->libname, start, end);
140 _dl_mprotect ((void *) start, end - start, PROT_READ) < 0) {
141 _dl_dprintf(2, "%s: cannot apply additional memory protection after relocation", l->libname);
146 /* This function's behavior must exactly match that
147 * in uClibc/ldso/util/ldd.c */
148 static struct elf_resolve *
149 search_for_named_library(const char *name, int secure, const char *path_list,
150 struct dyn_elf **rpnt)
153 char mylibname[2050];
154 struct elf_resolve *tpnt;
160 /* We need a writable copy of this string */
161 path = _dl_strdup(path_list);
163 _dl_dprintf(2, "Out of memory!\n");
167 /* Unlike ldd.c, don't bother to eliminate double //s */
169 /* Replace colons with zeros in path_list */
170 /* : at the beginning or end of path maps to CWD */
171 /* :: anywhere maps CWD */
182 _dl_strcpy(mylibname, path_n);
184 _dl_strcpy(mylibname, "."); /* Assume current dir if empty path */
185 _dl_strcat(mylibname, "/");
186 _dl_strcat(mylibname, name);
187 if ((tpnt = _dl_load_elf_shared_library(secure, rpnt, mylibname)) != NULL)
196 /* Check if the named library is already loaded... */
197 struct elf_resolve *_dl_check_if_named_library_is_loaded(const char *full_libname,
198 int trace_loaded_objects)
200 const char *pnt, *pnt1;
201 struct elf_resolve *tpnt1;
202 const char *libname, *libname2;
203 static const char libc[] = "libc.so.";
204 static const char aborted_wrong_lib[] = "%s: aborted attempt to load %s!\n";
206 pnt = libname = full_libname;
208 _dl_if_debug_dprint("Checking if '%s' is already loaded\n", full_libname);
209 /* quick hack to ensure mylibname buffer doesn't overflow. don't
210 allow full_libname or any directory to be longer than 1024. */
211 if (_dl_strlen(full_libname) > 1024)
214 /* Skip over any initial initial './' and '/' stuff to
215 * get the short form libname with no path garbage */
216 pnt1 = _dl_strrchr(pnt, '/');
221 /* Make sure they are not trying to load the wrong C library!
222 * This sometimes happens esp with shared libraries when the
223 * library path is somehow wrong! */
224 #define isdigit(c) (c >= '0' && c <= '9')
225 if ((_dl_strncmp(libname, libc, 8) == 0) && _dl_strlen(libname) >=8 &&
228 /* Abort attempts to load glibc, libc5, etc */
229 if ( libname[8]!='0') {
230 if (!trace_loaded_objects) {
231 _dl_dprintf(2, aborted_wrong_lib, libname, _dl_progname);
238 /* Critical step! Weed out duplicates early to avoid
239 * function aliasing, which wastes memory, and causes
240 * really bad things to happen with weaks and globals. */
241 for (tpnt1 = _dl_loaded_modules; tpnt1; tpnt1 = tpnt1->next) {
243 /* Skip over any initial initial './' and '/' stuff to
244 * get the short form libname with no path garbage */
245 libname2 = tpnt1->libname;
246 pnt1 = _dl_strrchr(libname2, '/');
251 if (_dl_strcmp(libname2, libname) == 0) {
252 /* Well, that was certainly easy */
261 /* Used to return error codes back to dlopen et. al. */
262 unsigned long _dl_error_number;
263 unsigned long _dl_internal_error_number;
265 struct elf_resolve *_dl_load_shared_library(int secure, struct dyn_elf **rpnt,
266 struct elf_resolve *tpnt, char *full_libname, int __attribute__((unused)) trace_loaded_objects)
269 struct elf_resolve *tpnt1;
272 _dl_internal_error_number = 0;
273 libname = full_libname;
275 /* quick hack to ensure mylibname buffer doesn't overflow. don't
276 allow full_libname or any directory to be longer than 1024. */
277 if (_dl_strlen(full_libname) > 1024)
280 /* Skip over any initial initial './' and '/' stuff to
281 * get the short form libname with no path garbage */
282 pnt1 = _dl_strrchr(libname, '/');
287 /* Critical step! Weed out duplicates early to avoid
288 * function aliasing, which wastes memory, and causes
289 * really bad things to happen with weaks and globals. */
290 if ((tpnt1=_dl_check_if_named_library_is_loaded(libname, trace_loaded_objects))!=NULL) {
291 tpnt1->usage_count++;
295 _dl_if_debug_dprint("\tfind library='%s'; searching\n", libname);
296 /* If the filename has any '/', try it straight and leave it at that.
297 For IBCS2 compatibility under linux, we substitute the string
298 /usr/i486-sysv4/lib for /usr/lib in library names. */
300 if (libname != full_libname) {
301 _dl_if_debug_dprint("\ttrying file='%s'\n", full_libname);
302 tpnt1 = _dl_load_elf_shared_library(secure, rpnt, full_libname);
310 * The ABI specifies that RPATH is searched before LD_LIBRARY_PATH or
311 * the default path of /usr/lib. Check in rpath directories.
313 pnt = (tpnt ? (char *) tpnt->dynamic_info[DT_RPATH] : NULL);
315 pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
316 _dl_if_debug_dprint("\tsearching RPATH='%s'\n", pnt);
317 if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
321 /* Check in LD_{ELF_}LIBRARY_PATH, if specified and allowed */
322 if (_dl_library_path) {
323 _dl_if_debug_dprint("\tsearching LD_LIBRARY_PATH='%s'\n", _dl_library_path);
324 if ((tpnt1 = search_for_named_library(libname, secure, _dl_library_path, rpnt)) != NULL)
330 * The ABI specifies that RUNPATH is searched after LD_LIBRARY_PATH.
332 pnt = (tpnt ? (char *)tpnt->dynamic_info[DT_RUNPATH] : NULL);
334 pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
335 _dl_if_debug_dprint("\tsearching RUNPATH='%s'\n", pnt);
336 if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
341 * Where should the cache be searched? There is no such concept in the
342 * ABI, so we have some flexibility here. For now, search it before
343 * the hard coded paths that follow (i.e before /lib and /usr/lib).
345 #ifdef __LDSO_CACHE_SUPPORT__
346 if (_dl_cache_addr != NULL && _dl_cache_addr != (caddr_t) - 1) {
348 header_t *header = (header_t *) _dl_cache_addr;
349 libentry_t *libent = (libentry_t *) & header[1];
350 char *strs = (char *) &libent[header->nlibs];
352 _dl_if_debug_dprint("\tsearching cache='%s'\n", LDSO_CACHE);
353 for (i = 0; i < header->nlibs; i++) {
354 if ((libent[i].flags == LIB_ELF ||
355 libent[i].flags == LIB_ELF_LIBC0 ||
356 libent[i].flags == LIB_ELF_LIBC5) &&
357 _dl_strcmp(libname, strs + libent[i].sooffset) == 0 &&
358 (tpnt1 = _dl_load_elf_shared_library(secure,
359 rpnt, strs + libent[i].liboffset)))
365 /* Look for libraries wherever the shared library loader
367 _dl_if_debug_dprint("\tsearching ldso dir='%s'\n", _dl_ldsopath);
368 if ((tpnt1 = search_for_named_library(libname, secure, _dl_ldsopath, rpnt)) != NULL)
374 /* Lastly, search the standard list of paths for the library.
375 This list must exactly match the list in uClibc/ldso/util/ldd.c */
376 _dl_if_debug_dprint("\tsearching full lib path list\n");
377 if ((tpnt1 = search_for_named_library(libname, secure,
378 UCLIBC_RUNTIME_PREFIX "lib:"
379 UCLIBC_RUNTIME_PREFIX "usr/lib"
380 #ifndef __LDSO_CACHE_SUPPORT__
381 ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
390 /* Well, we shot our wad on that one. All we can do now is punt */
391 if (_dl_internal_error_number)
392 _dl_error_number = _dl_internal_error_number;
394 _dl_error_number = LD_ERROR_NOFILE;
395 #if defined (__SUPPORT_LD_DEBUG__)
396 if(_dl_debug) _dl_dprintf(2, "Bummer: could not find '%s'!\n", libname);
403 * Read one ELF library into memory, mmap it into the correct locations and
404 * add the symbol info to the symbol chain. Perform any relocations that
408 struct elf_resolve *_dl_load_elf_shared_library(int secure,
409 struct dyn_elf **rpnt, char *libname)
412 unsigned long dynamic_addr = 0;
414 struct elf_resolve *tpnt;
416 char *status, *header;
417 unsigned long dynamic_info[DYNAMIC_SIZE];
419 unsigned long libaddr;
420 unsigned long minvma = 0xffffffff, maxvma = 0;
421 int i, flags, piclib, infile;
422 ElfW(Addr) relro_addr = 0;
423 size_t relro_size = 0;
425 /* If this file is already loaded, skip this step */
426 tpnt = _dl_check_hashed_files(libname);
429 (*rpnt)->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
430 _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
431 (*rpnt)->next->prev = (*rpnt);
432 *rpnt = (*rpnt)->next;
434 tpnt->symbol_scope = _dl_symbol_tables;
437 tpnt->libtype = elf_lib;
438 #if defined (__SUPPORT_LD_DEBUG__)
439 if(_dl_debug) _dl_dprintf(2, "file='%s'; already loaded\n", libname);
444 /* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
445 we don't load the library if it isn't setuid. */
450 if (_dl_stat(libname, &st) || !(st.st_mode & S_ISUID))
455 infile = _dl_open(libname, O_RDONLY, 0);
459 * NO! When we open shared libraries we may search several paths.
460 * it is inappropriate to generate an error here.
462 _dl_dprintf(2, "%s: can't open '%s'\n", _dl_progname, libname);
464 _dl_internal_error_number = LD_ERROR_NOFILE;
468 header = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
469 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
470 if (_dl_mmap_check_error(header)) {
471 _dl_dprintf(2, "%s: can't map '%s'\n", _dl_progname, libname);
472 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
477 _dl_read(infile, header, _dl_pagesize);
478 epnt = (ElfW(Ehdr) *) (intptr_t) header;
479 if (epnt->e_ident[0] != 0x7f ||
480 epnt->e_ident[1] != 'E' ||
481 epnt->e_ident[2] != 'L' ||
482 epnt->e_ident[3] != 'F')
484 _dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
486 _dl_internal_error_number = LD_ERROR_NOTELF;
488 _dl_munmap(header, _dl_pagesize);
492 if ((epnt->e_type != ET_DYN) || (epnt->e_machine != MAGIC1
494 && epnt->e_machine != MAGIC2
498 _dl_internal_error_number =
499 (epnt->e_type != ET_DYN ? LD_ERROR_NOTDYN : LD_ERROR_NOTMAGIC);
500 _dl_dprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
501 "\n", _dl_progname, libname);
503 _dl_munmap(header, _dl_pagesize);
507 ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
510 for (i = 0; i < epnt->e_phnum; i++) {
512 if (ppnt->p_type == PT_DYNAMIC) {
514 _dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
515 _dl_progname, libname);
516 dynamic_addr = ppnt->p_vaddr;
519 if (ppnt->p_type == PT_LOAD) {
520 /* See if this is a PIC library. */
521 if (i == 0 && ppnt->p_vaddr > 0x1000000) {
523 minvma = ppnt->p_vaddr;
525 if (piclib && ppnt->p_vaddr < minvma) {
526 minvma = ppnt->p_vaddr;
528 if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
529 maxvma = ppnt->p_vaddr + ppnt->p_memsz;
535 maxvma = (maxvma + ADDR_ALIGN) & ~ADDR_ALIGN;
536 minvma = minvma & ~0xffffU;
538 flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
542 status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
543 maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
544 if (_dl_mmap_check_error(status)) {
545 _dl_dprintf(2, "%s: can't map %s\n", _dl_progname, libname);
546 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
548 _dl_munmap(header, _dl_pagesize);
551 libaddr = (unsigned long) status;
554 /* Get the memory to store the library */
555 ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
557 for (i = 0; i < epnt->e_phnum; i++) {
558 if (ppnt->p_type == PT_GNU_RELRO) {
559 relro_addr = ppnt->p_vaddr;
560 relro_size = ppnt->p_memsz;
562 if (ppnt->p_type == PT_LOAD) {
564 /* See if this is a PIC library. */
565 if (i == 0 && ppnt->p_vaddr > 0x1000000) {
567 /* flags |= MAP_FIXED; */
572 if (ppnt->p_flags & PF_W) {
573 unsigned long map_size;
576 status = (char *) _dl_mmap((char *) ((piclib ? libaddr : 0) +
577 (ppnt->p_vaddr & PAGE_ALIGN)), (ppnt->p_vaddr & ADDR_ALIGN)
578 + ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags, infile,
579 ppnt->p_offset & OFFS_ALIGN);
581 if (_dl_mmap_check_error(status)) {
582 _dl_dprintf(2, "%s: can't map '%s'\n",
583 _dl_progname, libname);
584 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
585 _dl_munmap((char *) libaddr, maxvma - minvma);
587 _dl_munmap(header, _dl_pagesize);
591 /* Pad the last page with zeroes. */
592 cpnt = (char *) (status + (ppnt->p_vaddr & ADDR_ALIGN) +
594 while (((unsigned long) cpnt) & ADDR_ALIGN)
597 /* I am not quite sure if this is completely
598 * correct to do or not, but the basic way that
599 * we handle bss segments is that we mmap
600 * /dev/zero if there are any pages left over
601 * that are not mapped as part of the file */
603 map_size = (ppnt->p_vaddr + ppnt->p_filesz + ADDR_ALIGN) & PAGE_ALIGN;
605 if (map_size < ppnt->p_vaddr + ppnt->p_memsz)
606 status = (char *) _dl_mmap((char *) map_size +
607 (piclib ? libaddr : 0),
608 ppnt->p_vaddr + ppnt->p_memsz - map_size,
609 LXFLAGS(ppnt->p_flags), flags | MAP_ANONYMOUS, -1, 0);
611 status = (char *) _dl_mmap((char *) (ppnt->p_vaddr & PAGE_ALIGN)
612 + (piclib ? libaddr : 0), (ppnt->p_vaddr & ADDR_ALIGN) +
613 ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags,
614 infile, ppnt->p_offset & OFFS_ALIGN);
615 if (_dl_mmap_check_error(status)) {
616 _dl_dprintf(2, "%s: can't map '%s'\n", _dl_progname, libname);
617 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
618 _dl_munmap((char *) libaddr, maxvma - minvma);
620 _dl_munmap(header, _dl_pagesize);
624 /* if(libaddr == 0 && piclib) {
625 libaddr = (unsigned long) status;
633 /* For a non-PIC library, the addresses are all absolute */
635 dynamic_addr += (unsigned long) libaddr;
639 * OK, the ELF library is now loaded into VM in the correct locations
640 * The next step is to go through and do the dynamic linking (if needed).
643 /* Start by scanning the dynamic section to get all of the pointers */
646 _dl_internal_error_number = LD_ERROR_NODYNAMIC;
647 _dl_dprintf(2, "%s: '%s' is missing a dynamic section\n",
648 _dl_progname, libname);
649 _dl_munmap(header, _dl_pagesize);
653 dpnt = (Elf32_Dyn *) dynamic_addr;
654 _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
655 _dl_parse_dynamic_info(dpnt, dynamic_info, NULL, libaddr);
656 /* If the TEXTREL is set, this means that we need to make the pages
657 writable before we perform relocations. Do this now. They get set
660 if (dynamic_info[DT_TEXTREL]) {
661 #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
662 ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
663 for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
664 if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
665 _dl_mprotect((void *) ((piclib ? libaddr : 0) +
666 (ppnt->p_vaddr & PAGE_ALIGN)),
667 (ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
668 PROT_READ | PROT_WRITE | PROT_EXEC);
671 _dl_dprintf(_dl_debug_file, "Can't modify %s's text section. Use GCC option -fPIC for shared objects, please.\n",libname);
676 tpnt = _dl_add_elf_hash_table(libname, (char *) libaddr, dynamic_info,
678 tpnt->relro_addr = relro_addr;
679 tpnt->relro_size = relro_size;
680 tpnt->ppnt = (ElfW(Phdr) *)(intptr_t) (tpnt->loadaddr + epnt->e_phoff);
681 tpnt->n_phent = epnt->e_phnum;
684 * Add this object into the symbol chain
687 (*rpnt)->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
688 _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
689 (*rpnt)->next->prev = (*rpnt);
690 *rpnt = (*rpnt)->next;
692 tpnt->symbol_scope = _dl_symbol_tables;
695 tpnt->libtype = elf_lib;
698 * OK, the next thing we need to do is to insert the dynamic linker into
699 * the proper entry in the GOT so that the PLT symbols can be properly
703 lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
706 lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT]);
707 INIT_GOT(lpnt, tpnt);
710 #if defined (__SUPPORT_LD_DEBUG__)
712 _dl_dprintf(2, "\n\tfile='%s'; generating link map\n", libname);
713 _dl_dprintf(2, "\t\tdynamic: %x base: %x\n",
714 dynamic_addr, libaddr);
715 _dl_dprintf(2, "\t\t entry: %x phdr: %x phnum: %x\n\n",
716 epnt->e_entry + libaddr, tpnt->ppnt, tpnt->n_phent);
720 _dl_munmap(header, _dl_pagesize);
724 /* now_flag must be RTLD_NOW or zero */
725 int _dl_fixup(struct dyn_elf *rpnt, int now_flag)
728 struct elf_resolve *tpnt;
729 Elf32_Word reloc_size, reloc_addr, relative_count;
732 goof += _dl_fixup(rpnt->next, now_flag);
735 #if defined (__SUPPORT_LD_DEBUG__)
736 if(!(tpnt->init_flag & RELOCS_DONE))
737 _dl_if_debug_dprint("\nrelocation processing: %s\n", tpnt->libname);
740 if (unlikely(tpnt->dynamic_info[UNSUPPORTED_RELOC_TYPE])) {
741 _dl_if_debug_dprint("%s: can't handle %s relocation records\n",
742 _dl_progname, UNSUPPORTED_RELOC_STR);
747 reloc_size = tpnt->dynamic_info[DT_RELOC_TABLE_SIZE];
748 /* On some machines, notably SPARC & PPC, DT_REL* includes DT_JMPREL in its
749 range. Note that according to the ELF spec, this is completely legal! */
750 #ifdef ELF_MACHINE_PLTREL_OVERLAP
751 reloc_size -= tpnt->dynamic_info [DT_PLTRELSZ];
753 if (tpnt->dynamic_info[DT_RELOC_TABLE_ADDR] &&
754 !(tpnt->init_flag & RELOCS_DONE)) {
755 tpnt->init_flag |= RELOCS_DONE;
756 reloc_addr = tpnt->dynamic_info[DT_RELOC_TABLE_ADDR];
757 relative_count = tpnt->dynamic_info[DT_RELCONT_IDX];
758 if (relative_count) { /* Optimize the XX_RELATIVE relocations if possible */
759 reloc_size -= relative_count * sizeof(ELF_RELOC);
760 elf_machine_relative (tpnt->loadaddr, reloc_addr, relative_count);
761 reloc_addr += relative_count * sizeof(ELF_RELOC);
763 goof += _dl_parse_relocation_information(rpnt,
767 if (tpnt->dynamic_info[DT_BIND_NOW])
769 if (tpnt->dynamic_info[DT_JMPREL] &&
770 (!(tpnt->init_flag & JMP_RELOCS_DONE) ||
771 (now_flag && !(tpnt->rtld_flags & now_flag)))) {
772 tpnt->rtld_flags |= now_flag;
773 tpnt->init_flag |= JMP_RELOCS_DONE;
774 if (!(tpnt->rtld_flags & RTLD_NOW)) {
775 _dl_parse_lazy_relocation_information(rpnt,
776 tpnt->dynamic_info[DT_JMPREL],
777 tpnt->dynamic_info [DT_PLTRELSZ]);
779 goof += _dl_parse_relocation_information(rpnt,
780 tpnt->dynamic_info[DT_JMPREL],
781 tpnt->dynamic_info[DT_PLTRELSZ]);
787 /* Minimal printf which handles only %s, %d, and %x */
788 void _dl_dprintf(int fd, const char *fmt, ...)
792 char *start, *ptr, *string;
795 buf = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
796 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
797 if (_dl_mmap_check_error(buf)) {
798 _dl_write(fd, "mmap of a spare page failed!\n", 29);
807 if (_dl_strlen(fmt) >= (_dl_pagesize - 1)) {
808 _dl_write(fd, "overflow\n", 11);
812 _dl_strcpy(buf, fmt);
816 while (*ptr != '%' && *ptr) {
822 _dl_write(fd, start, _dl_strlen(start));
826 string = va_arg(args, char *);
829 _dl_write(fd, "(null)", 6);
831 _dl_write(fd, string, _dl_strlen(string));
838 num = va_arg(args, int);
840 string = _dl_simple_ltoa(tmp, num);
841 _dl_write(fd, string, _dl_strlen(string));
848 num = va_arg(args, int);
850 string = _dl_simple_ltoahex(tmp, num);
851 _dl_write(fd, string, _dl_strlen(string));
855 _dl_write(fd, "(null)", 6);
861 _dl_write(fd, start, _dl_strlen(start));
865 _dl_munmap(buf, _dl_pagesize);
869 char *_dl_strdup(const char *string)
874 len = _dl_strlen(string);
875 retval = _dl_malloc(len + 1);
876 _dl_strcpy(retval, string);
880 void _dl_parse_dynamic_info(Elf32_Dyn *dpnt, unsigned long dynamic_info[], void *debug_addr, Elf32_Addr load_off)
882 __dl_parse_dynamic_info(dpnt, dynamic_info, debug_addr, load_off);
885 #if ! defined LIBDL || (! defined PIC && ! defined __PIC__)
887 __dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info, size_t size, void *data), void *data)
889 struct elf_resolve *l;
890 struct dl_phdr_info info;
893 for (l = _dl_loaded_modules; l != NULL; l = l->next) {
894 info.dlpi_addr = l->loadaddr;
895 info.dlpi_name = l->libname;
896 info.dlpi_phdr = l->ppnt;
897 info.dlpi_phnum = l->n_phent;
898 ret = callback (&info, sizeof (struct dl_phdr_info), data);
904 strong_alias(__dl_iterate_phdr, dl_iterate_phdr);