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.
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);
318 * Make a writeable mapping of a segment, regardless of whether PF_W is
322 map_writeable (int infile, ElfW(Phdr) *ppnt, int piclib, int flags,
323 unsigned long libaddr)
325 int prot_flags = ppnt->p_flags | PF_W;
326 char *status, *retval;
329 unsigned long map_size;
331 char *piclib2map = NULL;
334 /* We might be able to avoid this call if memsz doesn't
335 require an additional page, but this would require mmap
336 to always return page-aligned addresses and a whole
337 number of pages allocated. Unfortunately on uClinux
338 may return misaligned addresses and may allocate
339 partial pages, so we may end up doing unnecessary mmap
342 This is what we could do if we knew mmap would always
343 return aligned pages:
345 ((ppnt->p_vaddr + ppnt->p_filesz + ADDR_ALIGN) &
346 PAGE_ALIGN) < ppnt->p_vaddr + ppnt->p_memsz)
348 Instead, we have to do this: */
349 ppnt->p_filesz < ppnt->p_memsz)
351 piclib2map = (char *)
352 _dl_mmap(0, (ppnt->p_vaddr & ADDR_ALIGN) + ppnt->p_memsz,
353 LXFLAGS(prot_flags), flags | MAP_ANONYMOUS, -1, 0);
354 if (_dl_mmap_check_error(piclib2map))
358 tryaddr = piclib == 2 ? piclib2map
359 : ((char*) (piclib ? libaddr : 0) +
360 (ppnt->p_vaddr & PAGE_ALIGN));
362 size = (ppnt->p_vaddr & ADDR_ALIGN) + ppnt->p_filesz;
364 /* For !MMU, mmap to fixed address will fail.
365 So instead of desperately call mmap and fail,
366 we set status to MAP_FAILED to save a call
368 #ifndef __ARCH_USE_MMU__
371 status = (char *) _dl_mmap
372 (tryaddr, size, LXFLAGS(prot_flags),
373 flags | (piclib2map ? MAP_FIXED : 0),
374 infile, ppnt->p_offset & OFFS_ALIGN);
375 #ifndef __ARCH_USE_MMU__
380 if (_dl_mmap_check_error(status) && piclib2map
381 && (_DL_PREAD (infile, tryaddr, size,
382 ppnt->p_offset & OFFS_ALIGN) == size))
385 if (_dl_mmap_check_error(status) || (tryaddr && tryaddr != status))
393 /* Now we want to allocate and zero-out any data from the end
394 of the region we mapped in from the file (filesz) to the
395 end of the loadable segment (memsz). We may need
396 additional pages for memsz, that we map in below, and we
397 can count on the kernel to zero them out, but we have to
398 zero out stuff in the last page that we mapped in from the
399 file. However, we can't assume to have actually obtained
400 full pages from the kernel, since we didn't ask for them,
401 and uClibc may not give us full pages for small
402 allocations. So only zero out up to memsz or the end of
403 the page, whichever comes first. */
405 /* CPNT is the beginning of the memsz portion not backed by
407 cpnt = (char *) (status + size);
409 /* MAP_SIZE is the address of the
410 beginning of the next page. */
411 map_size = (ppnt->p_vaddr + ppnt->p_filesz
412 + ADDR_ALIGN) & PAGE_ALIGN;
421 if (map_size < ppnt->p_vaddr + ppnt->p_memsz && !piclib2map) {
422 tryaddr = map_size + (char*)(piclib ? libaddr : 0);
423 status = (char *) _dl_mmap(tryaddr,
424 ppnt->p_vaddr + ppnt->p_memsz - map_size,
426 flags | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
427 if (_dl_mmap_check_error(status) || tryaddr != status)
434 * Read one ELF library into memory, mmap it into the correct locations and
435 * add the symbol info to the symbol chain. Perform any relocations that
439 struct elf_resolve *_dl_load_elf_shared_library(int secure,
440 struct dyn_elf **rpnt, const char *libname)
443 unsigned long dynamic_addr = 0;
445 struct elf_resolve *tpnt;
447 #if defined(USE_TLS) && USE_TLS
448 ElfW(Phdr) *tlsppnt = NULL;
450 char *status, *header;
451 unsigned long dynamic_info[DYNAMIC_SIZE];
453 unsigned long libaddr;
454 unsigned long minvma = 0xffffffff, maxvma = 0;
455 unsigned int rtld_flags;
456 int i, flags, piclib, infile;
457 ElfW(Addr) relro_addr = 0;
458 size_t relro_size = 0;
461 DL_LOADADDR_TYPE lib_loadaddr = 0;
462 DL_INIT_LOADADDR_EXTRA_DECLS
465 infile = _dl_open(libname, O_RDONLY, 0);
467 _dl_internal_error_number = LD_ERROR_NOFILE;
471 if (_dl_fstat(infile, &st) < 0) {
472 _dl_internal_error_number = LD_ERROR_NOFILE;
476 /* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
477 we don't load the library if it isn't setuid. */
479 if (!(st.st_mode & S_ISUID)) {
485 /* Check if file is already loaded */
486 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
487 if (tpnt->st_dev == st.st_dev && tpnt->st_ino == st.st_ino) {
494 header = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
495 MAP_PRIVATE | MAP_ANONYMOUS | MAP_UNINITIALIZE, -1, 0);
496 if (_dl_mmap_check_error(header)) {
497 _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
498 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
503 _dl_read(infile, header, _dl_pagesize);
504 epnt = (ElfW(Ehdr) *) (intptr_t) header;
505 p32 = (uint32_t*)&epnt->e_ident;
506 if (*p32 != ELFMAG_U32) {
507 _dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
509 _dl_internal_error_number = LD_ERROR_NOTELF;
511 _dl_munmap(header, _dl_pagesize);
515 if ((epnt->e_type != ET_DYN
516 #ifdef __LDSO_STANDALONE_SUPPORT__
517 && epnt->e_type != ET_EXEC
519 ) || (epnt->e_machine != MAGIC1
521 && epnt->e_machine != MAGIC2
525 _dl_internal_error_number =
526 (epnt->e_type != ET_DYN ? LD_ERROR_NOTDYN : LD_ERROR_NOTMAGIC);
527 _dl_dprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
528 "\n", _dl_progname, libname);
530 _dl_munmap(header, _dl_pagesize);
534 ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
537 for (i = 0; i < epnt->e_phnum; i++) {
539 if (ppnt->p_type == PT_DYNAMIC) {
541 _dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
542 _dl_progname, libname);
543 dynamic_addr = ppnt->p_vaddr;
546 if (ppnt->p_type == PT_LOAD) {
547 /* See if this is a PIC library. */
548 if (minvma == 0xffffffff && ppnt->p_vaddr > 0x1000000) {
550 minvma = ppnt->p_vaddr;
552 if (piclib && ppnt->p_vaddr < minvma) {
553 minvma = ppnt->p_vaddr;
555 if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
556 maxvma = ppnt->p_vaddr + ppnt->p_memsz;
559 if (ppnt->p_type == PT_TLS) {
560 #if defined(USE_TLS) && USE_TLS
561 if (ppnt->p_memsz == 0)
562 /* Nothing to do for an empty segment. */
565 /* Save for after 'tpnt' is actually allocated. */
569 * Yup, the user was an idiot and tried to sneak in a library with
570 * TLS in it and we don't support it. Let's fall on our own sword
571 * and scream at the luser while we die.
573 _dl_dprintf(2, "%s: '%s' library contains unsupported TLS\n",
574 _dl_progname, libname);
575 _dl_internal_error_number = LD_ERROR_TLS_FAILED;
577 _dl_munmap(header, _dl_pagesize);
584 #ifdef __LDSO_STANDALONE_SUPPORT__
585 if (epnt->e_type == ET_EXEC)
589 DL_CHECK_LIB_TYPE (epnt, piclib, _dl_progname, libname);
591 maxvma = (maxvma + ADDR_ALIGN) & PAGE_ALIGN;
592 minvma = minvma & ~ADDR_ALIGN;
594 flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
596 if (piclib == 0 || piclib == 1) {
597 status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
598 maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
599 if (_dl_mmap_check_error(status)) {
601 _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
602 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
604 _dl_munmap(header, _dl_pagesize);
607 libaddr = (unsigned long) status;
611 /* Get the memory to store the library */
612 ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
614 DL_INIT_LOADADDR(lib_loadaddr, libaddr - minvma, ppnt, epnt->e_phnum);
616 for (i = 0; i < epnt->e_phnum; i++) {
617 if (DL_IS_SPECIAL_SEGMENT (epnt, ppnt)) {
620 addr = DL_MAP_SEGMENT (epnt, ppnt, infile, flags);
623 DL_LOADADDR_UNMAP (lib_loadaddr, maxvma - minvma);
627 DL_INIT_LOADADDR_HDR (lib_loadaddr, addr, ppnt);
631 if (ppnt->p_type == PT_GNU_RELRO) {
632 relro_addr = ppnt->p_vaddr;
633 relro_size = ppnt->p_memsz;
635 if (ppnt->p_type == PT_LOAD) {
639 if (ppnt->p_flags & PF_W) {
640 status = map_writeable (infile, ppnt, piclib, flags, libaddr);
644 tryaddr = (piclib == 2 ? 0
645 : (char *) (ppnt->p_vaddr & PAGE_ALIGN)
646 + (piclib ? libaddr : lib_loadaddr));
647 size = (ppnt->p_vaddr & ADDR_ALIGN) + ppnt->p_filesz;
648 status = (char *) _dl_mmap
649 (tryaddr, size, LXFLAGS(ppnt->p_flags),
650 flags | (piclib == 2 ? MAP_EXECUTABLE
651 | MAP_DENYWRITE : 0),
652 infile, ppnt->p_offset & OFFS_ALIGN);
653 if (_dl_mmap_check_error(status)
654 || (tryaddr && tryaddr != status))
657 DL_INIT_LOADADDR_HDR(lib_loadaddr,
658 status + (ppnt->p_vaddr & ADDR_ALIGN),
661 /* if (libaddr == 0 && piclib) {
662 libaddr = (unsigned long) status;
670 * The dynamic_addr must be take into acount lib_loadaddr value, to note
671 * it is zero when the SO has been mapped to the elf's physical addr
674 dynamic_addr = (unsigned long) DL_RELOC_ADDR(lib_loadaddr, dynamic_addr);
678 * OK, the ELF library is now loaded into VM in the correct locations
679 * The next step is to go through and do the dynamic linking (if needed).
682 /* Start by scanning the dynamic section to get all of the pointers */
685 _dl_internal_error_number = LD_ERROR_NODYNAMIC;
686 _dl_dprintf(2, "%s: '%s' is missing a dynamic section\n",
687 _dl_progname, libname);
688 _dl_munmap(header, _dl_pagesize);
693 dpnt = (ElfW(Dyn) *) dynamic_addr;
694 _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
695 rtld_flags = _dl_parse_dynamic_info(dpnt, dynamic_info, NULL, lib_loadaddr);
696 /* If the TEXTREL is set, this means that we need to make the pages
697 writable before we perform relocations. Do this now. They get set
700 if (dynamic_info[DT_TEXTREL]) {
701 #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
702 ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
703 for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
704 if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W)) {
705 #ifdef __ARCH_USE_MMU__
706 _dl_mprotect((void *) ((piclib ? libaddr : lib_loadaddr) +
707 (ppnt->p_vaddr & PAGE_ALIGN)),
708 (ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
709 PROT_READ | PROT_WRITE | PROT_EXEC);
712 new_addr = map_writeable (infile, ppnt, piclib, flags, libaddr);
714 _dl_dprintf(_dl_debug_file, "Can't modify %s's text section.",
718 DL_UPDATE_LOADADDR_HDR(lib_loadaddr,
719 new_addr + (ppnt->p_vaddr & ADDR_ALIGN),
721 /* This has invalidated all pointers into the previously readonly segment.
722 Update any them to point into the remapped segment. */
723 _dl_parse_dynamic_info(dpnt, dynamic_info, NULL, lib_loadaddr);
728 _dl_dprintf(_dl_debug_file, "Can't modify %s's text section."
729 " Use GCC option -fPIC for shared objects, please.\n",
737 tpnt = _dl_add_elf_hash_table(libname, lib_loadaddr, dynamic_info,
739 tpnt->mapaddr = libaddr;
740 tpnt->relro_addr = relro_addr;
741 tpnt->relro_size = relro_size;
742 tpnt->st_dev = st.st_dev;
743 tpnt->st_ino = st.st_ino;
744 tpnt->ppnt = (ElfW(Phdr) *) DL_RELOC_ADDR(tpnt->mapaddr, epnt->e_phoff);
745 tpnt->n_phent = epnt->e_phnum;
746 tpnt->rtld_flags |= rtld_flags;
747 #ifdef __LDSO_STANDALONE_SUPPORT__
748 tpnt->l_entry = epnt->e_entry;
751 #if defined(USE_TLS) && USE_TLS
753 _dl_debug_early("Found TLS header for %s\n", libname);
754 # if NO_TLS_OFFSET != 0
755 tpnt->l_tls_offset = NO_TLS_OFFSET;
757 tpnt->l_tls_blocksize = tlsppnt->p_memsz;
758 tpnt->l_tls_align = tlsppnt->p_align;
759 if (tlsppnt->p_align == 0)
760 tpnt->l_tls_firstbyte_offset = 0;
762 tpnt->l_tls_firstbyte_offset = tlsppnt->p_vaddr &
763 (tlsppnt->p_align - 1);
764 tpnt->l_tls_initimage_size = tlsppnt->p_filesz;
765 tpnt->l_tls_initimage = (void *) tlsppnt->p_vaddr;
767 /* Assign the next available module ID. */
768 tpnt->l_tls_modid = _dl_next_tls_modid ();
770 /* We know the load address, so add it to the offset. */
771 #ifdef __LDSO_STANDALONE_SUPPORT__
772 if ((tpnt->l_tls_initimage != NULL) && piclib)
774 if (tpnt->l_tls_initimage != NULL)
777 # ifdef __SUPPORT_LD_DEBUG_EARLY__
778 unsigned int tmp = (unsigned int) tpnt->l_tls_initimage;
779 tpnt->l_tls_initimage = (char *) tlsppnt->p_vaddr + tpnt->loadaddr;
780 _dl_debug_early("Relocated TLS initial image from %x to %x (size = %x)\n", tmp, tpnt->l_tls_initimage, tpnt->l_tls_initimage_size);
783 tpnt->l_tls_initimage = (char *) tlsppnt->p_vaddr + tpnt->loadaddr;
790 * Add this object into the symbol chain
793 #ifdef __LDSO_STANDALONE_SUPPORT__
794 /* Do not create a new chain entry for the main executable */
798 (*rpnt)->next = _dl_malloc(sizeof(struct dyn_elf));
799 _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
800 (*rpnt)->next->prev = (*rpnt);
801 *rpnt = (*rpnt)->next;
804 /* When statically linked, the first time we dlopen a DSO
805 * the *rpnt is NULL, so we need to allocate memory for it,
806 * and initialize the _dl_symbol_table.
809 *rpnt = _dl_symbol_tables = _dl_malloc(sizeof(struct dyn_elf));
810 _dl_memset(*rpnt, 0, sizeof(struct dyn_elf));
815 #ifdef __LDSO_STANDALONE_SUPPORT__
816 tpnt->libtype = (epnt->e_type == ET_DYN) ? elf_lib : elf_executable;
818 tpnt->libtype = elf_lib;
822 * OK, the next thing we need to do is to insert the dynamic linker into
823 * the proper entry in the GOT so that the PLT symbols can be properly
827 lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
830 lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT]);
831 INIT_GOT(lpnt, tpnt);
835 /* Handle DSBT initialization */
837 struct elf_resolve *t, *ref;
838 int idx = tpnt->loadaddr.map->dsbt_index;
839 unsigned *dsbt = tpnt->loadaddr.map->dsbt_table;
842 if (!dynamic_info[DT_TEXTREL]) {
843 /* This DSO has not been assigned an index. */
844 _dl_dprintf(2, "%s: '%s' is missing a dsbt index assignment!\n",
845 _dl_progname, libname);
848 /* Find a dsbt table from another module. */
850 for (t = _dl_loaded_modules; t; t = t->next) {
851 if (ref == NULL && t != tpnt) {
856 idx = tpnt->loadaddr.map->dsbt_size;
858 if (!ref || ref->loadaddr.map->dsbt_table[idx] == NULL)
861 _dl_dprintf(2, "%s: '%s' caused DSBT table overflow!\n",
862 _dl_progname, libname);
865 _dl_if_debug_dprint("\n\tfile='%s'; assigned index %d\n",
867 tpnt->loadaddr.map->dsbt_index = idx;
872 * Setup dsbt slot for this module in dsbt of all modules.
875 for (t = _dl_loaded_modules; t; t = t->next) {
876 /* find a dsbt table from another module */
877 if (ref == NULL && t != tpnt) {
880 /* make sure index is not already used */
881 if (t->loadaddr.map->dsbt_table[idx]) {
882 struct elf_resolve *dup;
885 for (dup = _dl_loaded_modules; dup; dup = dup->next)
886 if (dup != tpnt && dup->loadaddr.map->dsbt_index == idx)
889 dup_name = dup->libname;
891 dup_name = "runtime linker";
893 dup_name = "unknown library";
894 _dl_dprintf(2, "%s: '%s' dsbt index %d already used by %s!\n",
895 _dl_progname, libname, idx, dup_name);
899 t->loadaddr.map->dsbt_table[idx] = (unsigned)dsbt;
902 _dl_memcpy(dsbt, ref->loadaddr.map->dsbt_table,
903 tpnt->loadaddr.map->dsbt_size * sizeof(unsigned *));
906 _dl_if_debug_dprint("\n\tfile='%s'; generating link map\n", libname);
907 _dl_if_debug_dprint("\t\tdynamic: %x base: %x\n", dynamic_addr, DL_LOADADDR_BASE(lib_loadaddr));
908 _dl_if_debug_dprint("\t\t entry: %x phdr: %x phnum: %x\n\n",
909 DL_RELOC_ADDR(lib_loadaddr, epnt->e_entry), tpnt->ppnt, tpnt->n_phent);
911 _dl_munmap(header, _dl_pagesize);
916 /* now_flag must be RTLD_NOW or zero */
917 int _dl_fixup(struct dyn_elf *rpnt, struct r_scope_elem *scope, int now_flag)
920 struct elf_resolve *tpnt;
921 ElfW(Word) reloc_size, relative_count;
922 ElfW(Addr) reloc_addr;
925 goof = _dl_fixup(rpnt->next, scope, now_flag);
930 if (!(tpnt->init_flag & RELOCS_DONE))
931 _dl_if_debug_dprint("relocation processing: %s\n", tpnt->libname);
933 if (unlikely(tpnt->dynamic_info[UNSUPPORTED_RELOC_TYPE])) {
934 _dl_if_debug_dprint("%s: can't handle %s relocation records\n",
935 _dl_progname, UNSUPPORTED_RELOC_STR);
940 reloc_size = tpnt->dynamic_info[DT_RELOC_TABLE_SIZE];
941 /* On some machines, notably SPARC & PPC, DT_REL* includes DT_JMPREL in its
942 range. Note that according to the ELF spec, this is completely legal! */
943 #ifdef ELF_MACHINE_PLTREL_OVERLAP
944 reloc_size -= tpnt->dynamic_info [DT_PLTRELSZ];
946 if (tpnt->dynamic_info[DT_RELOC_TABLE_ADDR] &&
947 !(tpnt->init_flag & RELOCS_DONE)) {
948 reloc_addr = tpnt->dynamic_info[DT_RELOC_TABLE_ADDR];
949 relative_count = tpnt->dynamic_info[DT_RELCONT_IDX];
950 if (relative_count) { /* Optimize the XX_RELATIVE relocations if possible */
951 reloc_size -= relative_count * sizeof(ELF_RELOC);
953 #ifdef __LDSO_PRELINK_SUPPORT__
954 || (!tpnt->dynamic_info[DT_GNU_PRELINKED_IDX])
957 elf_machine_relative(tpnt->loadaddr, reloc_addr, relative_count);
958 reloc_addr += relative_count * sizeof(ELF_RELOC);
960 goof += _dl_parse_relocation_information(rpnt, scope,
963 tpnt->init_flag |= RELOCS_DONE;
965 if (tpnt->dynamic_info[DT_BIND_NOW])
967 if (tpnt->dynamic_info[DT_JMPREL] &&
968 (!(tpnt->init_flag & JMP_RELOCS_DONE) ||
969 (now_flag && !(tpnt->rtld_flags & now_flag)))) {
970 tpnt->rtld_flags |= now_flag;
971 if (!(tpnt->rtld_flags & RTLD_NOW)) {
972 _dl_parse_lazy_relocation_information(rpnt,
973 tpnt->dynamic_info[DT_JMPREL],
974 tpnt->dynamic_info [DT_PLTRELSZ]);
976 goof += _dl_parse_relocation_information(rpnt, scope,
977 tpnt->dynamic_info[DT_JMPREL],
978 tpnt->dynamic_info[DT_PLTRELSZ]);
980 tpnt->init_flag |= JMP_RELOCS_DONE;
984 /* _dl_add_to_slotinfo is called by init_tls() for initial DSO
985 or by dlopen() for dynamically loaded DSO. */
986 #if defined(USE_TLS) && USE_TLS
987 /* Add object to slot information data if necessasy. */
988 if (tpnt->l_tls_blocksize != 0 && tls_init_tp_called)
989 _dl_add_to_slotinfo ((struct link_map *) tpnt);
995 /* Minimal printf which handles only %s, %d, and %x */
996 void _dl_dprintf(int fd, const char *fmt, ...)
1004 char *start, *ptr, *string;
1010 buf = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
1011 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1012 if (_dl_mmap_check_error(buf)) {
1013 _dl_write(fd, "mmap of a spare page failed!\n", 29);
1019 if (_dl_strlen(fmt) >= (_dl_pagesize - 1)) {
1020 _dl_write(fd, "overflow\n", 11);
1024 _dl_strcpy(buf, fmt);
1025 va_start(args, fmt);
1028 while (*ptr != '%' && *ptr) {
1034 _dl_write(fd, start, _dl_strlen(start));
1038 string = va_arg(args, char *);
1041 _dl_write(fd, "(null)", 6);
1043 _dl_write(fd, string, _dl_strlen(string));
1051 num = va_arg(args, long int);
1053 num = va_arg(args, int);
1055 string = _dl_simple_ltoa(tmp, num);
1056 _dl_write(fd, string, _dl_strlen(string));
1064 num = va_arg(args, long int);
1066 num = va_arg(args, int);
1068 string = _dl_simple_ltoahex(tmp, num);
1069 _dl_write(fd, string, _dl_strlen(string));
1073 _dl_write(fd, "(null)", 6);
1079 _dl_write(fd, start, _dl_strlen(start));
1083 _dl_munmap(buf, _dl_pagesize);
1087 char *_dl_strdup(const char *string)
1092 len = _dl_strlen(string);
1093 retval = _dl_malloc(len + 1);
1094 _dl_strcpy(retval, string);
1098 unsigned int _dl_parse_dynamic_info(ElfW(Dyn) *dpnt, unsigned long dynamic_info[],
1099 void *debug_addr, DL_LOADADDR_TYPE load_off)
1101 return __dl_parse_dynamic_info(dpnt, dynamic_info, debug_addr, load_off);