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 == MAP_FAILED)
50 else if (_dl_cache_addr != NULL)
53 if (_dl_stat(LDSO_CACHE, &st)
54 || (fd = _dl_open(LDSO_CACHE, O_RDONLY|O_CLOEXEC, 0)) < 0) {
55 _dl_cache_addr = MAP_FAILED; /* so we won't try again */
59 _dl_cache_size = st.st_size;
60 _dl_cache_addr = _dl_mmap(0, _dl_cache_size, PROT_READ, LDSO_CACHE_MMAP_FLAGS, 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 = MAP_FAILED;
103 int _dl_unmap_cache(void)
105 if (_dl_cache_addr == NULL || _dl_cache_addr == MAP_FAILED)
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) base = (ElfW(Addr)) DL_RELOC_ADDR(l->loadaddr, l->relro_addr);
122 ElfW(Addr) start = (base & PAGE_ALIGN);
123 ElfW(Addr) end = ((base + l->relro_size) & PAGE_ALIGN);
124 _dl_if_debug_dprint("RELRO protecting %s: start:%x, end:%x\n", l->libname, start, end);
126 _dl_mprotect ((void *) start, end - start, PROT_READ) < 0) {
127 _dl_dprintf(2, "%s: cannot apply additional memory protection after relocation", l->libname);
132 /* This function's behavior must exactly match that
133 * in uClibc/ldso/util/ldd.c */
134 static struct elf_resolve *
135 search_for_named_library(const char *name, int secure, const char *path_list,
136 struct dyn_elf **rpnt)
138 char *path, *path_n, *mylibname;
139 struct elf_resolve *tpnt;
145 /* We need a writable copy of this string, but we don't
146 * need this allocated permanently since we don't want
147 * to leak memory, so use alloca to put path on the stack */
148 done = _dl_strlen(path_list);
149 path = alloca(done + 1);
151 /* another bit of local storage */
152 mylibname = alloca(2050);
154 _dl_memcpy(path, path_list, done+1);
156 /* Unlike ldd.c, don't bother to eliminate double //s */
158 /* Replace colons with zeros in path_list */
159 /* : at the beginning or end of path maps to CWD */
160 /* :: 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 /* Used to return error codes back to dlopen et. al. */
187 unsigned long _dl_error_number;
188 unsigned long _dl_internal_error_number;
190 struct elf_resolve *_dl_load_shared_library(int secure, struct dyn_elf **rpnt,
191 struct elf_resolve *tpnt, char *full_libname, int __attribute__((unused)) trace_loaded_objects)
194 struct elf_resolve *tpnt1;
197 _dl_internal_error_number = 0;
198 libname = full_libname;
200 /* quick hack to ensure mylibname buffer doesn't overflow. don't
201 allow full_libname or any directory to be longer than 1024. */
202 if (_dl_strlen(full_libname) > 1024)
205 /* Skip over any initial initial './' and '/' stuff to
206 * get the short form libname with no path garbage */
207 pnt = _dl_strrchr(libname, '/');
212 _dl_if_debug_dprint("\tfind library='%s'; searching\n", libname);
213 /* If the filename has any '/', try it straight and leave it at that.
214 For IBCS2 compatibility under linux, we substitute the string
215 /usr/i486-sysv4/lib for /usr/lib in library names. */
217 if (libname != full_libname) {
218 _dl_if_debug_dprint("\ttrying file='%s'\n", full_libname);
219 tpnt1 = _dl_load_elf_shared_library(secure, rpnt, full_libname);
226 * The ABI specifies that RPATH is searched before LD_LIBRARY_PATH or
227 * the default path of /usr/lib. Check in rpath directories.
229 #ifdef __LDSO_RUNPATH__
230 pnt = (tpnt ? (char *) tpnt->dynamic_info[DT_RPATH] : NULL);
232 pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
233 _dl_if_debug_dprint("\tsearching RPATH='%s'\n", pnt);
234 if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
239 /* Check in LD_{ELF_}LIBRARY_PATH, if specified and allowed */
240 if (_dl_library_path) {
241 _dl_if_debug_dprint("\tsearching LD_LIBRARY_PATH='%s'\n", _dl_library_path);
242 if ((tpnt1 = search_for_named_library(libname, secure, _dl_library_path, rpnt)) != NULL)
249 * The ABI specifies that RUNPATH is searched after LD_LIBRARY_PATH.
251 #ifdef __LDSO_RUNPATH__
252 pnt = (tpnt ? (char *)tpnt->dynamic_info[DT_RUNPATH] : NULL);
254 pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
255 _dl_if_debug_dprint("\tsearching RUNPATH='%s'\n", pnt);
256 if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
262 * Where should the cache be searched? There is no such concept in the
263 * ABI, so we have some flexibility here. For now, search it before
264 * the hard coded paths that follow (i.e before /lib and /usr/lib).
266 #ifdef __LDSO_CACHE_SUPPORT__
267 if (_dl_cache_addr != NULL && _dl_cache_addr != MAP_FAILED) {
269 header_t *header = (header_t *) _dl_cache_addr;
270 libentry_t *libent = (libentry_t *) & header[1];
271 char *strs = (char *) &libent[header->nlibs];
273 _dl_if_debug_dprint("\tsearching cache='%s'\n", LDSO_CACHE);
274 for (i = 0; i < header->nlibs; i++) {
275 if ((libent[i].flags == LIB_ELF
276 || libent[i].flags == LIB_ELF_LIBC0
277 || libent[i].flags == LIB_ELF_LIBC5)
278 && _dl_strcmp(libname, strs + libent[i].sooffset) == 0
279 && (tpnt1 = _dl_load_elf_shared_library(secure, rpnt, strs + libent[i].liboffset))
287 /* Look for libraries wherever the shared library loader
289 _dl_if_debug_dprint("\tsearching ldso dir='%s'\n", _dl_ldsopath);
290 tpnt1 = search_for_named_library(libname, secure, _dl_ldsopath, rpnt);
294 /* Lastly, search the standard list of paths for the library.
295 This list must exactly match the list in uClibc/ldso/util/ldd.c */
296 _dl_if_debug_dprint("\tsearching full lib path list\n");
297 tpnt1 = search_for_named_library(libname, secure,
298 UCLIBC_RUNTIME_PREFIX "lib:"
299 UCLIBC_RUNTIME_PREFIX "usr/lib"
300 #ifndef __LDSO_CACHE_SUPPORT__
301 ":" 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 #if defined(USE_TLS) && USE_TLS
333 ElfW(Phdr) *tlsppnt = NULL;
335 char *status, *header;
336 unsigned long dynamic_info[DYNAMIC_SIZE];
338 unsigned long libaddr;
339 unsigned long minvma = 0xffffffff, maxvma = 0;
340 int i, flags, piclib, infile;
341 ElfW(Addr) relro_addr = 0;
342 size_t relro_size = 0;
345 DL_LOADADDR_TYPE lib_loadaddr;
346 DL_INIT_LOADADDR_EXTRA_DECLS
349 infile = _dl_open(libname, O_RDONLY, 0);
351 _dl_internal_error_number = LD_ERROR_NOFILE;
355 if (_dl_fstat(infile, &st) < 0) {
356 _dl_internal_error_number = LD_ERROR_NOFILE;
360 /* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
361 we don't load the library if it isn't setuid. */
363 if (!(st.st_mode & S_ISUID)) {
369 /* Check if file is already loaded */
370 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
371 #ifndef __NOT_FOR_L4__
372 if (_dl_cap_equal(tpnt->st_dev, st.st_dev)) {
374 if (tpnt->st_dev == st.st_dev && tpnt->st_ino == st.st_ino) {
382 header = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
383 MAP_PRIVATE | MAP_ANONYMOUS | MAP_UNINITIALIZE, -1, 0);
384 if (_dl_mmap_check_error(header)) {
385 _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
386 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
391 _dl_read(infile, header, _dl_pagesize);
392 epnt = (ElfW(Ehdr) *) (intptr_t) header;
393 p32 = (uint32_t*)&epnt->e_ident;
394 if (*p32 != ELFMAG_U32) {
395 _dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
397 _dl_internal_error_number = LD_ERROR_NOTELF;
399 _dl_munmap(header, _dl_pagesize);
403 if ((epnt->e_type != ET_DYN) || (epnt->e_machine != MAGIC1
405 && epnt->e_machine != MAGIC2
409 _dl_internal_error_number =
410 (epnt->e_type != ET_DYN ? LD_ERROR_NOTDYN : LD_ERROR_NOTMAGIC);
411 _dl_dprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
412 "\n", _dl_progname, libname);
414 _dl_munmap(header, _dl_pagesize);
418 ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
421 for (i = 0; i < epnt->e_phnum; i++) {
423 if (ppnt->p_type == PT_DYNAMIC) {
425 _dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
426 _dl_progname, libname);
427 dynamic_addr = ppnt->p_vaddr;
430 if (ppnt->p_type == PT_LOAD) {
431 /* See if this is a PIC library. */
432 if (i == 0 && ppnt->p_vaddr > 0x1000000) {
434 minvma = ppnt->p_vaddr;
436 if (piclib && ppnt->p_vaddr < minvma) {
437 minvma = ppnt->p_vaddr;
439 if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
440 maxvma = ppnt->p_vaddr + ppnt->p_memsz;
443 if (ppnt->p_type == PT_TLS) {
444 #if defined(USE_TLS) && USE_TLS
445 if (ppnt->p_memsz == 0)
446 /* Nothing to do for an empty segment. */
449 /* Save for after 'tpnt' is actually allocated. */
453 * Yup, the user was an idiot and tried to sneak in a library with
454 * TLS in it and we don't support it. Let's fall on our own sword
455 * and scream at the luser while we die.
457 _dl_dprintf(2, "%s: '%s' library contains unsupported TLS\n",
458 _dl_progname, libname);
459 _dl_internal_error_number = LD_ERROR_TLS_FAILED;
461 _dl_munmap(header, _dl_pagesize);
468 DL_CHECK_LIB_TYPE (epnt, piclib, _dl_progname, libname);
470 maxvma = (maxvma + ADDR_ALIGN) & PAGE_ALIGN;
471 minvma = minvma & ~0xffffU;
473 flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
477 if (piclib == 0 || piclib == 1) {
478 #ifdef __NOT_FOR_L4__
479 status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
480 maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
482 status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
483 maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS | 0x1000000, -1, 0);
485 if (_dl_mmap_check_error(status)) {
486 _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
487 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
489 _dl_munmap(header, _dl_pagesize);
492 libaddr = (unsigned long) status;
496 /* Get the memory to store the library */
497 ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
499 DL_INIT_LOADADDR(lib_loadaddr, libaddr, ppnt, epnt->e_phnum);
501 for (i = 0; i < epnt->e_phnum; i++) {
502 if (DL_IS_SPECIAL_SEGMENT (epnt, ppnt)) {
505 addr = DL_MAP_SEGMENT (epnt, ppnt, infile, flags);
509 DL_INIT_LOADADDR_HDR (lib_loadaddr, addr, ppnt);
513 if (ppnt->p_type == PT_GNU_RELRO) {
514 relro_addr = ppnt->p_vaddr;
515 relro_size = ppnt->p_memsz;
517 if (ppnt->p_type == PT_LOAD) {
521 /* See if this is a PIC library. */
522 if (i == 0 && ppnt->p_vaddr > 0x1000000) {
524 /* flags |= MAP_FIXED; */
527 if (ppnt->p_flags & PF_W) {
528 unsigned long map_size;
530 char *piclib2map = 0;
533 /* We might be able to avoid this
534 call if memsz doesn't require
535 an additional page, but this
536 would require mmap to always
537 return page-aligned addresses
538 and a whole number of pages
539 allocated. Unfortunately on
540 uClinux may return misaligned
541 addresses and may allocate
542 partial pages, so we may end up
543 doing unnecessary mmap calls.
545 This is what we could do if we
546 knew mmap would always return
549 ((ppnt->p_vaddr + ppnt->p_filesz
552 < ppnt->p_vaddr + ppnt->p_memsz)
554 Instead, we have to do this: */
555 ppnt->p_filesz < ppnt->p_memsz)
557 piclib2map = (char *)
558 _dl_mmap(0, (ppnt->p_vaddr & ADDR_ALIGN)
560 LXFLAGS(ppnt->p_flags),
561 flags | MAP_ANONYMOUS, -1, 0);
562 if (_dl_mmap_check_error(piclib2map))
565 (lib_loadaddr, piclib2map
566 + (ppnt->p_vaddr & ADDR_ALIGN), ppnt);
569 tryaddr = piclib == 2 ? piclib2map
570 : ((char*) (piclib ? libaddr : 0) +
571 (ppnt->p_vaddr & PAGE_ALIGN));
573 size = (ppnt->p_vaddr & ADDR_ALIGN)
576 /* For !MMU, mmap to fixed address will fail.
577 So instead of desperately call mmap and fail,
578 we set status to MAP_FAILED to save a call
580 #ifndef __ARCH_USE_MMU__
583 status = (char *) _dl_mmap
584 (tryaddr, size, LXFLAGS(ppnt->p_flags),
585 flags | (piclib2map ? MAP_FIXED : 0),
586 infile, ppnt->p_offset & OFFS_ALIGN);
587 #ifndef __ARCH_USE_MMU__
592 if (_dl_mmap_check_error(status) && piclib2map
593 && (_DL_PREAD (infile, tryaddr, size,
594 ppnt->p_offset & OFFS_ALIGN)
598 if (_dl_mmap_check_error(status)
599 || (tryaddr && tryaddr != status)) {
601 _dl_dprintf(2, "%s:%i: can't map '%s'\n",
602 _dl_progname, __LINE__, libname);
603 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
604 DL_LOADADDR_UNMAP (lib_loadaddr, maxvma - minvma);
606 _dl_munmap(header, _dl_pagesize);
612 (lib_loadaddr, status
613 + (ppnt->p_vaddr & ADDR_ALIGN), ppnt);
615 /* Now we want to allocate and
616 zero-out any data from the end of
617 the region we mapped in from the
618 file (filesz) to the end of the
619 loadable segment (memsz). We may
620 need additional pages for memsz,
621 that we map in below, and we can
622 count on the kernel to zero them
623 out, but we have to zero out stuff
624 in the last page that we mapped in
625 from the file. However, we can't
626 assume to have actually obtained
627 full pages from the kernel, since
628 we didn't ask for them, and uClibc
629 may not give us full pages for
630 small allocations. So only zero
631 out up to memsz or the end of the
632 page, whichever comes first. */
634 /* CPNT is the beginning of the memsz
635 portion not backed by filesz. */
636 cpnt = (char *) (status + size);
638 /* MAP_SIZE is the address of the
639 beginning of the next page. */
640 map_size = (ppnt->p_vaddr + ppnt->p_filesz
641 + ADDR_ALIGN) & PAGE_ALIGN;
644 # define MIN(a,b) ((a) < (b) ? (a) : (b))
653 if (map_size < ppnt->p_vaddr + ppnt->p_memsz
655 tryaddr = map_size + (char*)(piclib ? libaddr : 0);
656 status = (char *) _dl_mmap(tryaddr,
657 ppnt->p_vaddr + ppnt->p_memsz - map_size,
658 LXFLAGS(ppnt->p_flags), flags | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
659 if (_dl_mmap_check_error(status)
660 || tryaddr != status)
664 tryaddr = (piclib == 2 ? 0
665 : (char *) (ppnt->p_vaddr & PAGE_ALIGN)
666 + (piclib ? libaddr : 0));
667 size = (ppnt->p_vaddr & ADDR_ALIGN) + ppnt->p_filesz;
668 status = (char *) _dl_mmap
669 (tryaddr, size, LXFLAGS(ppnt->p_flags),
670 flags | (piclib == 2 ? MAP_EXECUTABLE
671 | MAP_DENYWRITE : 0),
672 infile, ppnt->p_offset & OFFS_ALIGN);
673 if (_dl_mmap_check_error(status)
674 || (tryaddr && tryaddr != status))
677 (lib_loadaddr, status
678 + (ppnt->p_vaddr & ADDR_ALIGN), ppnt);
681 /* if (libaddr == 0 && piclib) {
682 libaddr = (unsigned long) status;
690 /* For a non-PIC library, the addresses are all absolute */
692 dynamic_addr = (unsigned long) DL_RELOC_ADDR(lib_loadaddr, dynamic_addr);
696 * OK, the ELF library is now loaded into VM in the correct locations
697 * The next step is to go through and do the dynamic linking (if needed).
700 /* Start by scanning the dynamic section to get all of the pointers */
703 _dl_internal_error_number = LD_ERROR_NODYNAMIC;
704 _dl_dprintf(2, "%s: '%s' is missing a dynamic section\n",
705 _dl_progname, libname);
706 _dl_munmap(header, _dl_pagesize);
710 dpnt = (ElfW(Dyn) *) dynamic_addr;
711 _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
712 _dl_parse_dynamic_info(dpnt, dynamic_info, NULL, lib_loadaddr);
713 /* If the TEXTREL is set, this means that we need to make the pages
714 writable before we perform relocations. Do this now. They get set
717 if (dynamic_info[DT_TEXTREL]) {
718 #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
719 ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
720 for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
721 if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W)) {
722 _dl_mprotect((void *) ((piclib ? libaddr : 0) +
723 (ppnt->p_vaddr & PAGE_ALIGN)),
724 (ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
725 PROT_READ | PROT_WRITE | PROT_EXEC);
729 _dl_dprintf(_dl_debug_file, "Can't modify %s's text section."
730 " Use GCC option -fPIC for shared objects, please.\n",
736 tpnt = _dl_add_elf_hash_table(libname, lib_loadaddr, dynamic_info,
738 tpnt->relro_addr = relro_addr;
739 tpnt->relro_size = relro_size;
740 tpnt->st_dev = st.st_dev;
741 tpnt->st_ino = st.st_ino;
742 tpnt->ppnt = (ElfW(Phdr) *) DL_RELOC_ADDR(tpnt->loadaddr, epnt->e_phoff);
743 tpnt->n_phent = epnt->e_phnum;
745 #if defined(USE_TLS) && USE_TLS
747 _dl_debug_early("Found TLS header for %s\n", libname);
748 #if NO_TLS_OFFSET != 0
749 tpnt->l_tls_offset = NO_TLS_OFFSET;
751 tpnt->l_tls_blocksize = tlsppnt->p_memsz;
752 tpnt->l_tls_align = tlsppnt->p_align;
753 if (tlsppnt->p_align == 0)
754 tpnt->l_tls_firstbyte_offset = 0;
756 tpnt->l_tls_firstbyte_offset = tlsppnt->p_vaddr &
757 (tlsppnt->p_align - 1);
758 tpnt->l_tls_initimage_size = tlsppnt->p_filesz;
759 tpnt->l_tls_initimage = (void *) tlsppnt->p_vaddr;
761 /* Assign the next available module ID. */
762 tpnt->l_tls_modid = _dl_next_tls_modid ();
764 /* We know the load address, so add it to the offset. */
765 if (tpnt->l_tls_initimage != NULL)
767 unsigned int tmp = (unsigned int) tpnt->l_tls_initimage;
768 tpnt->l_tls_initimage = (char *) tlsppnt->p_vaddr + tpnt->loadaddr;
769 _dl_debug_early("Relocated TLS initial image from %x to %x (size = %x)\n", tmp, tpnt->l_tls_initimage, tpnt->l_tls_initimage_size);
776 * Add this object into the symbol chain
779 (*rpnt)->next = _dl_malloc(sizeof(struct dyn_elf));
780 _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
781 (*rpnt)->next->prev = (*rpnt);
782 *rpnt = (*rpnt)->next;
785 /* When statically linked, the first time we dlopen a DSO
786 * the *rpnt is NULL, so we need to allocate memory for it,
787 * and initialize the _dl_symbol_table.
790 *rpnt = _dl_symbol_tables = _dl_malloc(sizeof(struct dyn_elf));
791 _dl_memset(*rpnt, 0, sizeof(struct dyn_elf));
795 tpnt->symbol_scope = _dl_symbol_tables;
797 tpnt->libtype = elf_lib;
800 * OK, the next thing we need to do is to insert the dynamic linker into
801 * the proper entry in the GOT so that the PLT symbols can be properly
805 lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
808 lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT]);
809 INIT_GOT(lpnt, tpnt);
812 _dl_if_debug_dprint("\n\tfile='%s'; generating link map\n", libname);
813 _dl_if_debug_dprint("\t\tdynamic: %x base: %x\n", dynamic_addr, DL_LOADADDR_BASE(lib_loadaddr));
814 _dl_if_debug_dprint("\t\t entry: %x phdr: %x phnum: %x\n\n",
815 DL_RELOC_ADDR(lib_loadaddr, epnt->e_entry), tpnt->ppnt, tpnt->n_phent);
817 _dl_munmap(header, _dl_pagesize);
822 /* now_flag must be RTLD_NOW or zero */
823 int _dl_fixup(struct dyn_elf *rpnt, int now_flag)
826 struct elf_resolve *tpnt;
827 ElfW(Word) reloc_size, relative_count;
828 ElfW(Addr) reloc_addr;
831 goof = _dl_fixup(rpnt->next, now_flag);
836 if (!(tpnt->init_flag & RELOCS_DONE))
837 _dl_if_debug_dprint("relocation processing: %s\n", tpnt->libname);
839 if (unlikely(tpnt->dynamic_info[UNSUPPORTED_RELOC_TYPE])) {
840 _dl_if_debug_dprint("%s: can't handle %s relocation records\n",
841 _dl_progname, UNSUPPORTED_RELOC_STR);
846 reloc_size = tpnt->dynamic_info[DT_RELOC_TABLE_SIZE];
847 /* On some machines, notably SPARC & PPC, DT_REL* includes DT_JMPREL in its
848 range. Note that according to the ELF spec, this is completely legal! */
849 #ifdef ELF_MACHINE_PLTREL_OVERLAP
850 reloc_size -= tpnt->dynamic_info [DT_PLTRELSZ];
852 if (tpnt->dynamic_info[DT_RELOC_TABLE_ADDR] &&
853 !(tpnt->init_flag & RELOCS_DONE)) {
854 reloc_addr = tpnt->dynamic_info[DT_RELOC_TABLE_ADDR];
855 relative_count = tpnt->dynamic_info[DT_RELCONT_IDX];
856 if (relative_count) { /* Optimize the XX_RELATIVE relocations if possible */
857 reloc_size -= relative_count * sizeof(ELF_RELOC);
858 elf_machine_relative(tpnt->loadaddr, reloc_addr, relative_count);
859 reloc_addr += relative_count * sizeof(ELF_RELOC);
861 goof += _dl_parse_relocation_information(rpnt,
864 tpnt->init_flag |= RELOCS_DONE;
866 if (tpnt->dynamic_info[DT_BIND_NOW])
868 if (tpnt->dynamic_info[DT_JMPREL] &&
869 (!(tpnt->init_flag & JMP_RELOCS_DONE) ||
870 (now_flag && !(tpnt->rtld_flags & now_flag)))) {
871 tpnt->rtld_flags |= now_flag;
872 if (!(tpnt->rtld_flags & RTLD_NOW)) {
873 _dl_parse_lazy_relocation_information(rpnt,
874 tpnt->dynamic_info[DT_JMPREL],
875 tpnt->dynamic_info [DT_PLTRELSZ]);
877 goof += _dl_parse_relocation_information(rpnt,
878 tpnt->dynamic_info[DT_JMPREL],
879 tpnt->dynamic_info[DT_PLTRELSZ]);
881 tpnt->init_flag |= JMP_RELOCS_DONE;
885 /* _dl_add_to_slotinfo is called by init_tls() for initial DSO
886 or by dlopen() for dynamically loaded DSO. */
887 #if defined(USE_TLS) && USE_TLS
888 /* Add object to slot information data if necessasy. */
889 if (tpnt->l_tls_blocksize != 0 && tls_init_tp_called)
890 _dl_add_to_slotinfo ((struct link_map *) tpnt);
896 #include <l4/sys/utcb.h>
899 /* Minimal printf which handles only %s, %d, and %x */
900 void _dl_dprintf(int fd, const char *fmt, ...)
908 char *start, *ptr, *string;
915 l4_msg_regs_t store_mr;
916 l4_buf_regs_t store_br;
917 l4_msg_regs_t *mr = l4_utcb_mr();
918 l4_buf_regs_t *br = l4_utcb_br();
920 _dl_memcpy(&store_mr, mr, sizeof(store_mr));
921 _dl_memcpy(&store_br, br, sizeof(store_br));
924 buf = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
925 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
926 if (_dl_mmap_check_error(buf)) {
927 _dl_write(fd, "mmap of a spare page failed!\n", 29);
933 if (_dl_strlen(fmt) >= (_dl_pagesize - 1)) {
934 _dl_write(fd, "overflow\n", 11);
938 _dl_strcpy(buf, fmt);
942 while (*ptr != '%' && *ptr) {
948 _dl_write(fd, start, _dl_strlen(start));
952 string = va_arg(args, char *);
955 _dl_write(fd, "(null)", 6);
957 _dl_write(fd, string, _dl_strlen(string));
965 num = va_arg(args, long int);
967 num = va_arg(args, int);
969 string = _dl_simple_ltoa(tmp, num);
970 _dl_write(fd, string, _dl_strlen(string));
978 num = va_arg(args, long int);
980 num = va_arg(args, int);
982 string = _dl_simple_ltoahex(tmp, num);
983 _dl_write(fd, string, _dl_strlen(string));
987 _dl_write(fd, "(null)", 6);
993 _dl_write(fd, start, _dl_strlen(start));
997 _dl_munmap(buf, _dl_pagesize);
999 _dl_memcpy(mr, &store_mr, sizeof(store_mr));
1000 _dl_memcpy(br, &store_br, sizeof(store_br));
1005 char *_dl_strdup(const char *string)
1010 len = _dl_strlen(string);
1011 retval = _dl_malloc(len + 1);
1012 _dl_strcpy(retval, string);
1016 void _dl_parse_dynamic_info(ElfW(Dyn) *dpnt, unsigned long dynamic_info[],
1017 void *debug_addr, DL_LOADADDR_TYPE load_off)
1019 __dl_parse_dynamic_info(dpnt, dynamic_info, debug_addr, load_off);