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-2006 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:%i: can't map '%s'\n",
64 _dl_progname, __LINE__, 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 _dl_if_debug_dprint("RELRO protecting %s: start:%x, end:%x\n", l->libname, start, end);
127 _dl_mprotect ((void *) start, end - start, PROT_READ) < 0) {
128 _dl_dprintf(2, "%s: cannot apply additional memory protection after relocation", l->libname);
133 /* This function's behavior must exactly match that
134 * in uClibc/ldso/util/ldd.c */
135 static struct elf_resolve *
136 search_for_named_library(const char *name, int secure, const char *path_list,
137 struct dyn_elf **rpnt)
140 char mylibname[2050];
141 struct elf_resolve *tpnt;
147 /* We need a writable copy of this string */
148 path = _dl_strdup(path_list);
150 _dl_dprintf(2, "Out of memory!\n");
154 /* Unlike ldd.c, don't bother to eliminate double //s */
156 /* Replace colons with zeros in path_list */
157 /* : at the beginning or end of path maps to CWD */
158 /* :: anywhere maps CWD */
169 _dl_strcpy(mylibname, path_n);
171 _dl_strcpy(mylibname, "."); /* Assume current dir if empty path */
172 _dl_strcat(mylibname, "/");
173 _dl_strcat(mylibname, name);
174 if ((tpnt = _dl_load_elf_shared_library(secure, rpnt, mylibname)) != NULL)
183 /* Used to return error codes back to dlopen et. al. */
184 unsigned long _dl_error_number;
185 unsigned long _dl_internal_error_number;
187 struct elf_resolve *_dl_load_shared_library(int secure, struct dyn_elf **rpnt,
188 struct elf_resolve *tpnt, char *full_libname, int __attribute__((unused)) trace_loaded_objects)
191 struct elf_resolve *tpnt1;
194 _dl_internal_error_number = 0;
195 libname = full_libname;
197 /* quick hack to ensure mylibname buffer doesn't overflow. don't
198 allow full_libname or any directory to be longer than 1024. */
199 if (_dl_strlen(full_libname) > 1024)
202 /* Skip over any initial initial './' and '/' stuff to
203 * get the short form libname with no path garbage */
204 pnt = _dl_strrchr(libname, '/');
209 _dl_if_debug_dprint("\tfind library='%s'; searching\n", libname);
210 /* If the filename has any '/', try it straight and leave it at that.
211 For IBCS2 compatibility under linux, we substitute the string
212 /usr/i486-sysv4/lib for /usr/lib in library names. */
214 if (libname != full_libname) {
215 _dl_if_debug_dprint("\ttrying file='%s'\n", full_libname);
216 tpnt1 = _dl_load_elf_shared_library(secure, rpnt, full_libname);
223 * The ABI specifies that RPATH is searched before LD_LIBRARY_PATH or
224 * the default path of /usr/lib. Check in rpath directories.
226 #ifdef __LDSO_RUNPATH__
227 pnt = (tpnt ? (char *) tpnt->dynamic_info[DT_RPATH] : NULL);
229 pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
230 _dl_if_debug_dprint("\tsearching RPATH='%s'\n", pnt);
231 if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
236 /* Check in LD_{ELF_}LIBRARY_PATH, if specified and allowed */
237 if (_dl_library_path) {
238 _dl_if_debug_dprint("\tsearching LD_LIBRARY_PATH='%s'\n", _dl_library_path);
239 if ((tpnt1 = search_for_named_library(libname, secure, _dl_library_path, rpnt)) != NULL)
246 * The ABI specifies that RUNPATH is searched after LD_LIBRARY_PATH.
248 #ifdef __LDSO_RUNPATH__
249 pnt = (tpnt ? (char *)tpnt->dynamic_info[DT_RUNPATH] : NULL);
251 pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
252 _dl_if_debug_dprint("\tsearching RUNPATH='%s'\n", pnt);
253 if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
259 * Where should the cache be searched? There is no such concept in the
260 * ABI, so we have some flexibility here. For now, search it before
261 * the hard coded paths that follow (i.e before /lib and /usr/lib).
263 #ifdef __LDSO_CACHE_SUPPORT__
264 if (_dl_cache_addr != NULL && _dl_cache_addr != (caddr_t) - 1) {
266 header_t *header = (header_t *) _dl_cache_addr;
267 libentry_t *libent = (libentry_t *) & header[1];
268 char *strs = (char *) &libent[header->nlibs];
270 _dl_if_debug_dprint("\tsearching cache='%s'\n", LDSO_CACHE);
271 for (i = 0; i < header->nlibs; i++) {
272 if ((libent[i].flags == LIB_ELF ||
273 libent[i].flags == LIB_ELF_LIBC0 ||
274 libent[i].flags == LIB_ELF_LIBC5) &&
275 _dl_strcmp(libname, strs + libent[i].sooffset) == 0 &&
276 (tpnt1 = _dl_load_elf_shared_library(secure,
277 rpnt, strs + libent[i].liboffset)))
283 /* Look for libraries wherever the shared library loader
285 _dl_if_debug_dprint("\tsearching ldso dir='%s'\n", _dl_ldsopath);
286 if ((tpnt1 = search_for_named_library(libname, secure, _dl_ldsopath, rpnt)) != NULL)
292 /* Lastly, search the standard list of paths for the library.
293 This list must exactly match the list in uClibc/ldso/util/ldd.c */
294 _dl_if_debug_dprint("\tsearching full lib path list\n");
295 if ((tpnt1 = search_for_named_library(libname, secure,
296 UCLIBC_RUNTIME_PREFIX "lib:"
297 UCLIBC_RUNTIME_PREFIX "usr/lib"
298 #ifndef __LDSO_CACHE_SUPPORT__
299 ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
308 /* Well, we shot our wad on that one. All we can do now is punt */
309 if (_dl_internal_error_number)
310 _dl_error_number = _dl_internal_error_number;
312 _dl_error_number = LD_ERROR_NOFILE;
313 _dl_if_debug_dprint("Bummer: could not find '%s'!\n", libname);
319 * Read one ELF library into memory, mmap it into the correct locations and
320 * add the symbol info to the symbol chain. Perform any relocations that
324 struct elf_resolve *_dl_load_elf_shared_library(int secure,
325 struct dyn_elf **rpnt, char *libname)
328 unsigned long dynamic_addr = 0;
330 struct elf_resolve *tpnt;
332 char *status, *header;
333 unsigned long dynamic_info[DYNAMIC_SIZE];
335 unsigned long libaddr;
336 unsigned long minvma = 0xffffffff, maxvma = 0;
337 int i, flags, piclib, infile;
338 ElfW(Addr) relro_addr = 0;
339 size_t relro_size = 0;
343 infile = _dl_open(libname, O_RDONLY, 0);
345 _dl_internal_error_number = LD_ERROR_NOFILE;
349 if (_dl_fstat(infile, &st) < 0) {
350 _dl_internal_error_number = LD_ERROR_NOFILE;
354 /* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
355 we don't load the library if it isn't setuid. */
357 if (!(st.st_mode & S_ISUID)) {
362 /* Check if file is already loaded */
363 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
364 if(tpnt->st_dev == st.st_dev && tpnt->st_ino == st.st_ino) {
371 header = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
372 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
373 if (_dl_mmap_check_error(header)) {
374 _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
375 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
380 _dl_read(infile, header, _dl_pagesize);
381 epnt = (ElfW(Ehdr) *) (intptr_t) header;
382 if (epnt->e_ident[0] != 0x7f ||
383 epnt->e_ident[1] != 'E' ||
384 epnt->e_ident[2] != 'L' ||
385 epnt->e_ident[3] != 'F')
387 _dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
389 _dl_internal_error_number = LD_ERROR_NOTELF;
391 _dl_munmap(header, _dl_pagesize);
395 if ((epnt->e_type != ET_DYN) || (epnt->e_machine != MAGIC1
397 && epnt->e_machine != MAGIC2
401 _dl_internal_error_number =
402 (epnt->e_type != ET_DYN ? LD_ERROR_NOTDYN : LD_ERROR_NOTMAGIC);
403 _dl_dprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
404 "\n", _dl_progname, libname);
406 _dl_munmap(header, _dl_pagesize);
410 ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
413 for (i = 0; i < epnt->e_phnum; i++) {
415 if (ppnt->p_type == PT_DYNAMIC) {
417 _dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
418 _dl_progname, libname);
419 dynamic_addr = ppnt->p_vaddr;
422 if (ppnt->p_type == PT_LOAD) {
423 /* See if this is a PIC library. */
424 if (i == 0 && ppnt->p_vaddr > 0x1000000) {
426 minvma = ppnt->p_vaddr;
428 if (piclib && ppnt->p_vaddr < minvma) {
429 minvma = ppnt->p_vaddr;
431 if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
432 maxvma = ppnt->p_vaddr + ppnt->p_memsz;
438 maxvma = (maxvma + ADDR_ALIGN) & ~ADDR_ALIGN;
439 minvma = minvma & ~0xffffU;
441 flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
445 status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
446 maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
447 if (_dl_mmap_check_error(status)) {
448 _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
449 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
451 _dl_munmap(header, _dl_pagesize);
454 libaddr = (unsigned long) status;
457 /* Get the memory to store the library */
458 ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
460 for (i = 0; i < epnt->e_phnum; i++) {
461 if (ppnt->p_type == PT_GNU_RELRO) {
462 relro_addr = ppnt->p_vaddr;
463 relro_size = ppnt->p_memsz;
465 if (ppnt->p_type == PT_LOAD) {
467 /* See if this is a PIC library. */
468 if (i == 0 && ppnt->p_vaddr > 0x1000000) {
470 /* flags |= MAP_FIXED; */
475 if (ppnt->p_flags & PF_W) {
476 unsigned long map_size;
479 status = (char *) _dl_mmap((char *) ((piclib ? libaddr : 0) +
480 (ppnt->p_vaddr & PAGE_ALIGN)), (ppnt->p_vaddr & ADDR_ALIGN)
481 + ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags, infile,
482 ppnt->p_offset & OFFS_ALIGN);
484 if (_dl_mmap_check_error(status)) {
485 _dl_dprintf(2, "%s:%i: can't map '%s'\n",
486 _dl_progname, __LINE__, libname);
487 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
488 _dl_munmap((char *) libaddr, maxvma - minvma);
490 _dl_munmap(header, _dl_pagesize);
494 /* Pad the last page with zeroes. */
495 cpnt = (char *) (status + (ppnt->p_vaddr & ADDR_ALIGN) +
497 while (((unsigned long) cpnt) & ADDR_ALIGN)
500 /* I am not quite sure if this is completely
501 * correct to do or not, but the basic way that
502 * we handle bss segments is that we mmap
503 * /dev/zero if there are any pages left over
504 * that are not mapped as part of the file */
506 map_size = (ppnt->p_vaddr + ppnt->p_filesz + ADDR_ALIGN) & PAGE_ALIGN;
508 if (map_size < ppnt->p_vaddr + ppnt->p_memsz)
509 status = (char *) _dl_mmap((char *) map_size +
510 (piclib ? libaddr : 0),
511 ppnt->p_vaddr + ppnt->p_memsz - map_size,
512 LXFLAGS(ppnt->p_flags), flags | MAP_ANONYMOUS, -1, 0);
514 status = (char *) _dl_mmap((char *) (ppnt->p_vaddr & PAGE_ALIGN)
515 + (piclib ? libaddr : 0), (ppnt->p_vaddr & ADDR_ALIGN) +
516 ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags,
517 infile, ppnt->p_offset & OFFS_ALIGN);
518 if (_dl_mmap_check_error(status)) {
519 _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
520 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
521 _dl_munmap((char *) libaddr, maxvma - minvma);
523 _dl_munmap(header, _dl_pagesize);
527 /* if(libaddr == 0 && piclib) {
528 libaddr = (unsigned long) status;
536 /* For a non-PIC library, the addresses are all absolute */
538 dynamic_addr += (unsigned long) libaddr;
542 * OK, the ELF library is now loaded into VM in the correct locations
543 * The next step is to go through and do the dynamic linking (if needed).
546 /* Start by scanning the dynamic section to get all of the pointers */
549 _dl_internal_error_number = LD_ERROR_NODYNAMIC;
550 _dl_dprintf(2, "%s: '%s' is missing a dynamic section\n",
551 _dl_progname, libname);
552 _dl_munmap(header, _dl_pagesize);
556 dpnt = (ElfW(Dyn) *) dynamic_addr;
557 _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
558 _dl_parse_dynamic_info(dpnt, dynamic_info, NULL, libaddr);
559 /* If the TEXTREL is set, this means that we need to make the pages
560 writable before we perform relocations. Do this now. They get set
563 if (dynamic_info[DT_TEXTREL]) {
564 #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
565 ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
566 for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
567 if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
568 _dl_mprotect((void *) ((piclib ? libaddr : 0) +
569 (ppnt->p_vaddr & PAGE_ALIGN)),
570 (ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
571 PROT_READ | PROT_WRITE | PROT_EXEC);
574 _dl_dprintf(_dl_debug_file, "Can't modify %s's text section. Use GCC option -fPIC for shared objects, please.\n",libname);
579 tpnt = _dl_add_elf_hash_table(libname, (char *) libaddr, dynamic_info,
581 tpnt->relro_addr = relro_addr;
582 tpnt->relro_size = relro_size;
583 tpnt->st_dev = st.st_dev;
584 tpnt->st_ino = st.st_ino;
585 tpnt->ppnt = (ElfW(Phdr) *)(intptr_t) (tpnt->loadaddr + epnt->e_phoff);
586 tpnt->n_phent = epnt->e_phnum;
589 * Add this object into the symbol chain
592 (*rpnt)->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
593 _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
594 (*rpnt)->next->prev = (*rpnt);
595 *rpnt = (*rpnt)->next;
597 tpnt->symbol_scope = _dl_symbol_tables;
600 tpnt->libtype = elf_lib;
603 * OK, the next thing we need to do is to insert the dynamic linker into
604 * the proper entry in the GOT so that the PLT symbols can be properly
608 lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
611 lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT]);
612 INIT_GOT(lpnt, tpnt);
615 _dl_if_debug_dprint("\n\tfile='%s'; generating link map\n", libname);
616 _dl_if_debug_dprint("\t\tdynamic: %x base: %x\n", dynamic_addr, libaddr);
617 _dl_if_debug_dprint("\t\t entry: %x phdr: %x phnum: %x\n\n",
618 epnt->e_entry + libaddr, tpnt->ppnt, tpnt->n_phent);
620 _dl_munmap(header, _dl_pagesize);
625 /* now_flag must be RTLD_NOW or zero */
626 int _dl_fixup(struct dyn_elf *rpnt, int now_flag)
629 struct elf_resolve *tpnt;
630 ElfW(Word) reloc_size, relative_count;
631 ElfW(Addr) reloc_addr;
634 goof = _dl_fixup(rpnt->next, now_flag);
639 if(!(tpnt->init_flag & RELOCS_DONE))
640 _dl_if_debug_dprint("relocation processing: %s\n", tpnt->libname);
642 if (unlikely(tpnt->dynamic_info[UNSUPPORTED_RELOC_TYPE])) {
643 _dl_if_debug_dprint("%s: can't handle %s relocation records\n",
644 _dl_progname, UNSUPPORTED_RELOC_STR);
649 reloc_size = tpnt->dynamic_info[DT_RELOC_TABLE_SIZE];
650 /* On some machines, notably SPARC & PPC, DT_REL* includes DT_JMPREL in its
651 range. Note that according to the ELF spec, this is completely legal! */
652 #ifdef ELF_MACHINE_PLTREL_OVERLAP
653 reloc_size -= tpnt->dynamic_info [DT_PLTRELSZ];
655 if (tpnt->dynamic_info[DT_RELOC_TABLE_ADDR] &&
656 !(tpnt->init_flag & RELOCS_DONE)) {
657 reloc_addr = tpnt->dynamic_info[DT_RELOC_TABLE_ADDR];
658 relative_count = tpnt->dynamic_info[DT_RELCONT_IDX];
659 if (relative_count) { /* Optimize the XX_RELATIVE relocations if possible */
660 reloc_size -= relative_count * sizeof(ELF_RELOC);
661 elf_machine_relative(tpnt->loadaddr, reloc_addr, relative_count);
662 reloc_addr += relative_count * sizeof(ELF_RELOC);
664 goof += _dl_parse_relocation_information(rpnt,
667 tpnt->init_flag |= RELOCS_DONE;
669 if (tpnt->dynamic_info[DT_BIND_NOW])
671 if (tpnt->dynamic_info[DT_JMPREL] &&
672 (!(tpnt->init_flag & JMP_RELOCS_DONE) ||
673 (now_flag && !(tpnt->rtld_flags & now_flag)))) {
674 tpnt->rtld_flags |= now_flag;
675 if (!(tpnt->rtld_flags & RTLD_NOW)) {
676 _dl_parse_lazy_relocation_information(rpnt,
677 tpnt->dynamic_info[DT_JMPREL],
678 tpnt->dynamic_info [DT_PLTRELSZ]);
680 goof += _dl_parse_relocation_information(rpnt,
681 tpnt->dynamic_info[DT_JMPREL],
682 tpnt->dynamic_info[DT_PLTRELSZ]);
684 tpnt->init_flag |= JMP_RELOCS_DONE;
689 /* Minimal printf which handles only %s, %d, and %x */
690 void _dl_dprintf(int fd, const char *fmt, ...)
698 char *start, *ptr, *string;
704 buf = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
705 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
706 if (_dl_mmap_check_error(buf)) {
707 _dl_write(fd, "mmap of a spare page failed!\n", 29);
713 if (_dl_strlen(fmt) >= (_dl_pagesize - 1)) {
714 _dl_write(fd, "overflow\n", 11);
718 _dl_strcpy(buf, fmt);
722 while (*ptr != '%' && *ptr) {
728 _dl_write(fd, start, _dl_strlen(start));
732 string = va_arg(args, char *);
735 _dl_write(fd, "(null)", 6);
737 _dl_write(fd, string, _dl_strlen(string));
745 num = va_arg(args, long int);
747 num = va_arg(args, int);
749 string = _dl_simple_ltoa(tmp, num);
750 _dl_write(fd, string, _dl_strlen(string));
758 num = va_arg(args, long int);
760 num = va_arg(args, int);
762 string = _dl_simple_ltoahex(tmp, num);
763 _dl_write(fd, string, _dl_strlen(string));
767 _dl_write(fd, "(null)", 6);
773 _dl_write(fd, start, _dl_strlen(start));
777 _dl_munmap(buf, _dl_pagesize);
781 char *_dl_strdup(const char *string)
786 len = _dl_strlen(string);
787 retval = _dl_malloc(len + 1);
788 _dl_strcpy(retval, string);
792 void _dl_parse_dynamic_info(ElfW(Dyn) *dpnt, unsigned long dynamic_info[], void *debug_addr, ElfW(Addr) load_off)
794 __dl_parse_dynamic_info(dpnt, dynamic_info, debug_addr, load_off);
797 /* we want this in ldso.so and libdl.a but nowhere else */
799 #if defined IS_IN_rtld || (defined IS_IN_libdl && ! defined SHARED)
800 extern __typeof(dl_iterate_phdr) __dl_iterate_phdr;
802 __dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info, size_t size, void *data), void *data)
804 struct elf_resolve *l;
805 struct dl_phdr_info info;
808 for (l = _dl_loaded_modules; l != NULL; l = l->next) {
809 info.dlpi_addr = l->loadaddr;
810 info.dlpi_name = l->libname;
811 info.dlpi_phdr = l->ppnt;
812 info.dlpi_phnum = l->n_phent;
813 ret = callback (&info, sizeof (struct dl_phdr_info), data);
819 strong_alias(__dl_iterate_phdr, dl_iterate_phdr)