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
, unsigned int rflags
, const char *path_list
,
136 struct dyn_elf
**rpnt
, const char* origin
)
139 struct elf_resolve
*tpnt
;
146 /* another bit of local storage */
147 mylibname
= alloca(2050);
149 /* Unlike ldd.c, don't bother to eliminate double //s */
151 /* Replace colons with zeros in path_list */
152 /* : at the beginning or end of path maps to CWD */
153 /* :: anywhere maps CWD */
155 for (p
= path_list
; p
!= NULL
; p
= pn
) {
156 pn
= _dl_strchr(p
+ 1, ':');
161 plen
= _dl_strlen(p
);
163 if (plen
>= 7 && _dl_memcmp(p
, "$ORIGIN", 7) == 0) {
165 /* $ORIGIN is not expanded for SUID/GUID programs
166 (except if it is $ORIGIN alone) */
167 if ((rflags
& __RTLD_SECURE
) && plen
!= 7)
171 for (olen
= _dl_strlen(origin
) - 1; olen
>= 0 && origin
[olen
] != '/'; olen
--)
175 _dl_memcpy(&mylibname
[0], origin
, olen
);
176 _dl_memcpy(&mylibname
[olen
], p
+ 7, plen
- 7);
177 mylibname
[olen
+ plen
- 7] = 0;
178 } else if (plen
!= 0) {
179 _dl_memcpy(mylibname
, p
, plen
);
182 _dl_strcpy(mylibname
, ".");
184 _dl_strcat(mylibname
, "/");
185 _dl_strcat(mylibname
, name
);
186 #ifdef __LDSO_SAFE_RUNPATH__
187 if (*mylibname
== '/')
189 if ((tpnt
= _dl_load_elf_shared_library(rflags
, rpnt
, mylibname
)) != NULL
)
195 /* Used to return error codes back to dlopen et. al. */
196 unsigned long _dl_error_number
;
197 unsigned long _dl_internal_error_number
;
199 struct elf_resolve
*_dl_load_shared_library(unsigned int rflags
, struct dyn_elf
**rpnt
,
200 struct elf_resolve
*tpnt
, char *full_libname
, int attribute_unused trace_loaded_objects
)
203 struct elf_resolve
*tpnt1
;
206 _dl_internal_error_number
= 0;
207 libname
= 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 pnt
= _dl_strrchr(libname
, '/');
221 _dl_if_debug_dprint("\tfind library='%s'; searching\n", libname
);
222 /* If the filename has any '/', try it straight and leave it at that.
223 For IBCS2 compatibility under linux, we substitute the string
224 /usr/i486-sysv4/lib for /usr/lib in library names. */
226 if (libname
!= full_libname
) {
227 _dl_if_debug_dprint("\ttrying file='%s'\n", full_libname
);
228 tpnt1
= _dl_load_elf_shared_library(rflags
, rpnt
, full_libname
);
235 * The ABI specifies that RPATH is searched before LD_LIBRARY_PATH or
236 * the default path of /usr/lib. Check in rpath directories.
238 #ifdef __LDSO_RUNPATH__
239 pnt
= (tpnt
? (char *) tpnt
->dynamic_info
[DT_RPATH
] : NULL
);
241 pnt
+= (unsigned long) tpnt
->dynamic_info
[DT_STRTAB
];
242 _dl_if_debug_dprint("\tsearching RPATH='%s'\n", pnt
);
243 if ((tpnt1
= search_for_named_library(libname
, rflags
, pnt
, rpnt
,
244 tpnt
->libname
)) != NULL
)
250 #ifdef __LDSO_LD_LIBRARY_PATH__
251 /* Check in LD_{ELF_}LIBRARY_PATH, if specified and allowed */
252 if (_dl_library_path
) {
253 _dl_if_debug_dprint("\tsearching LD_LIBRARY_PATH='%s'\n", _dl_library_path
);
254 if ((tpnt1
= search_for_named_library(libname
, rflags
, _dl_library_path
, rpnt
, NULL
)) != NULL
)
261 * The ABI specifies that RUNPATH is searched after LD_LIBRARY_PATH.
263 #ifdef __LDSO_RUNPATH__
264 pnt
= (tpnt
? (char *)tpnt
->dynamic_info
[DT_RUNPATH
] : NULL
);
266 pnt
+= (unsigned long) tpnt
->dynamic_info
[DT_STRTAB
];
267 _dl_if_debug_dprint("\tsearching RUNPATH='%s'\n", pnt
);
268 if ((tpnt1
= search_for_named_library(libname
, rflags
, pnt
, rpnt
, NULL
)) != NULL
)
271 #ifdef __LDSO_RUNPATH_OF_EXECUTABLE__
273 * Try the DT_RPATH of the executable itself.
275 pnt
= (char *) _dl_loaded_modules
->dynamic_info
[DT_RPATH
];
277 pnt
+= (unsigned long) _dl_loaded_modules
->dynamic_info
[DT_STRTAB
];
278 _dl_if_debug_dprint("\tsearching exe's RPATH='%s'\n", pnt
);
279 if ((tpnt1
= search_for_named_library(libname
, rflags
, pnt
, rpnt
, NULL
)) != NULL
)
286 * Where should the cache be searched? There is no such concept in the
287 * ABI, so we have some flexibility here. For now, search it before
288 * the hard coded paths that follow (i.e before /lib and /usr/lib).
290 #ifdef __LDSO_CACHE_SUPPORT__
291 if (_dl_cache_addr
!= NULL
&& _dl_cache_addr
!= MAP_FAILED
) {
293 header_t
*header
= (header_t
*) _dl_cache_addr
;
294 libentry_t
*libent
= (libentry_t
*) & header
[1];
295 char *strs
= (char *) &libent
[header
->nlibs
];
297 _dl_if_debug_dprint("\tsearching cache='%s'\n", LDSO_CACHE
);
298 for (i
= 0; i
< header
->nlibs
; i
++) {
299 if ((libent
[i
].flags
== LIB_ELF
300 || libent
[i
].flags
== LIB_ELF_LIBC0
301 || libent
[i
].flags
== LIB_ELF_LIBC5
)
302 && _dl_strcmp(libname
, strs
+ libent
[i
].sooffset
) == 0
303 && (tpnt1
= _dl_load_elf_shared_library(rflags
, rpnt
, strs
+ libent
[i
].liboffset
))
310 #if defined SHARED && defined __LDSO_SEARCH_INTERP_PATH__
311 /* Look for libraries wherever the shared library loader
313 _dl_if_debug_dprint("\tsearching ldso dir='%s'\n", _dl_ldsopath
);
314 tpnt1
= search_for_named_library(libname
, rflags
, _dl_ldsopath
, rpnt
, NULL
);
318 /* Lastly, search the standard list of paths for the library.
319 This list must exactly match the list in uClibc/ldso/util/ldd.c */
320 _dl_if_debug_dprint("\tsearching full lib path list\n");
321 tpnt1
= search_for_named_library(libname
, rflags
,
322 UCLIBC_RUNTIME_PREFIX
"lib:"
323 UCLIBC_RUNTIME_PREFIX
"usr/lib"
324 #ifndef __LDSO_CACHE_SUPPORT__
325 ":" UCLIBC_RUNTIME_PREFIX
"usr/X11R6/lib"
331 #ifdef __LDSO_RUNPATH_OF_EXECUTABLE__
332 /* Very last resort, try the executable's DT_RUNPATH and DT_RPATH */
333 /* http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#shobj_dependencies
334 * The set of directories specified by a given DT_RUNPATH entry is
335 * used to find only the immediate dependencies of the executable or
336 * shared object containing the DT_RUNPATH entry. That is, it is
337 * used only for those dependencies contained in the DT_NEEDED
338 * entries of the dynamic structure containing the DT_RUNPATH entry,
339 * itself. One object's DT_RUNPATH entry does not affect the search
340 * for any other object's dependencies.
342 * glibc (around 2.19) violates this and the usual suspects are
343 * abusing this bug^Wrelaxed, user-friendly behaviour.
346 pnt
= (char *) _dl_loaded_modules
->dynamic_info
[DT_RUNPATH
];
348 pnt
+= (unsigned long) _dl_loaded_modules
->dynamic_info
[DT_STRTAB
];
349 _dl_if_debug_dprint("\tsearching exe's RUNPATH='%s'\n", pnt
);
350 if ((tpnt1
= search_for_named_library(libname
, rflags
, pnt
, rpnt
, NULL
)) != NULL
)
353 pnt
= (char *) _dl_loaded_modules
->dynamic_info
[DT_RPATH
];
355 pnt
+= (unsigned long) _dl_loaded_modules
->dynamic_info
[DT_STRTAB
];
356 _dl_if_debug_dprint("\tsearching exe's RPATH='%s'\n", pnt
);
357 if ((tpnt1
= search_for_named_library(libname
, rflags
, pnt
, rpnt
, NULL
)) != NULL
)
364 /* Well, we shot our wad on that one. All we can do now is punt */
365 if (_dl_internal_error_number
)
366 _dl_error_number
= _dl_internal_error_number
;
368 _dl_error_number
= LD_ERROR_NOFILE
;
369 _dl_if_debug_dprint("Bummer: could not find '%s'!\n", libname
);
373 /* Define the _dl_library_offset for the architectures that need it */
377 * Make a writeable mapping of a segment, regardless of whether PF_W is
381 map_writeable (int infile
, ElfW(Phdr
) *ppnt
, int piclib
, int flags
,
382 unsigned long libaddr
)
384 int prot_flags
= ppnt
->p_flags
| PF_W
;
385 char *status
, *retval
;
388 unsigned long map_size
;
390 char *piclib2map
= NULL
;
393 /* We might be able to avoid this call if memsz doesn't
394 require an additional page, but this would require mmap
395 to always return page-aligned addresses and a whole
396 number of pages allocated. Unfortunately on uClinux
397 may return misaligned addresses and may allocate
398 partial pages, so we may end up doing unnecessary mmap
401 This is what we could do if we knew mmap would always
402 return aligned pages:
404 ((ppnt->p_vaddr + ppnt->p_filesz + ADDR_ALIGN) &
405 PAGE_ALIGN) < ppnt->p_vaddr + ppnt->p_memsz)
407 Instead, we have to do this: */
408 ppnt
->p_filesz
< ppnt
->p_memsz
)
410 piclib2map
= (char *)
411 _dl_mmap(0, (ppnt
->p_vaddr
& ADDR_ALIGN
) + ppnt
->p_memsz
,
412 LXFLAGS(prot_flags
), flags
| MAP_ANONYMOUS
, -1, 0);
413 if (_dl_mmap_check_error(piclib2map
))
417 tryaddr
= piclib
== 2 ? piclib2map
418 : ((char *) (piclib
? libaddr
: DL_GET_LIB_OFFSET()) +
419 (ppnt
->p_vaddr
& PAGE_ALIGN
));
421 size
= (ppnt
->p_vaddr
& ADDR_ALIGN
) + ppnt
->p_filesz
;
423 /* For !MMU, mmap to fixed address will fail.
424 So instead of desperately call mmap and fail,
425 we set status to MAP_FAILED to save a call
427 #ifndef __ARCH_USE_MMU__
430 status
= (char *) _dl_mmap
431 (tryaddr
, size
, LXFLAGS(prot_flags
),
432 flags
| (piclib2map
? MAP_FIXED
: 0),
433 infile
, ppnt
->p_offset
& OFFS_ALIGN
);
434 #ifndef __ARCH_USE_MMU__
439 if (_dl_mmap_check_error(status
) && piclib2map
440 && (_DL_PREAD (infile
, tryaddr
, size
,
441 ppnt
->p_offset
& OFFS_ALIGN
) == size
))
444 if (_dl_mmap_check_error(status
) || (tryaddr
&& tryaddr
!= status
))
452 /* Now we want to allocate and zero-out any data from the end
453 of the region we mapped in from the file (filesz) to the
454 end of the loadable segment (memsz). We may need
455 additional pages for memsz, that we map in below, and we
456 can count on the kernel to zero them out, but we have to
457 zero out stuff in the last page that we mapped in from the
458 file. However, we can't assume to have actually obtained
459 full pages from the kernel, since we didn't ask for them,
460 and uClibc may not give us full pages for small
461 allocations. So only zero out up to memsz or the end of
462 the page, whichever comes first. */
464 /* CPNT is the beginning of the memsz portion not backed by
466 cpnt
= (char *) (status
+ size
);
468 /* MAP_SIZE is the address of the
469 beginning of the next page. */
470 map_size
= (ppnt
->p_vaddr
+ ppnt
->p_filesz
471 + ADDR_ALIGN
) & PAGE_ALIGN
;
480 if (map_size
< ppnt
->p_vaddr
+ ppnt
->p_memsz
&& !piclib2map
) {
481 tryaddr
= map_size
+ (char*)(piclib
? libaddr
: 0);
482 status
= (char *) _dl_mmap(tryaddr
,
483 ppnt
->p_vaddr
+ ppnt
->p_memsz
- map_size
,
485 flags
| MAP_ANONYMOUS
| MAP_FIXED
, -1, 0);
486 if (_dl_mmap_check_error(status
) || tryaddr
!= status
)
493 * Read one ELF library into memory, mmap it into the correct locations and
494 * add the symbol info to the symbol chain. Perform any relocations that
498 struct elf_resolve
*_dl_load_elf_shared_library(unsigned int rflags
,
499 struct dyn_elf
**rpnt
, const char *libname
)
502 unsigned long dynamic_addr
= 0;
504 struct elf_resolve
*tpnt
;
506 #if defined(USE_TLS) && USE_TLS
507 ElfW(Phdr
) *tlsppnt
= NULL
;
509 char *status
, *header
;
510 unsigned long dynamic_info
[DYNAMIC_SIZE
];
512 unsigned long libaddr
;
513 unsigned long minvma
= 0xffffffff, maxvma
= 0;
514 unsigned int rtld_flags
;
515 int i
, flags
, piclib
, infile
;
516 ElfW(Addr
) relro_addr
= 0;
517 size_t relro_size
= 0;
520 DL_LOADADDR_TYPE lib_loadaddr
;
521 DL_INIT_LOADADDR_EXTRA_DECLS
524 infile
= _dl_open(libname
, O_RDONLY
, 0);
526 _dl_internal_error_number
= LD_ERROR_NOFILE
;
530 if (_dl_fstat(infile
, &st
) < 0) {
531 _dl_internal_error_number
= LD_ERROR_NOFILE
;
535 /* If we are in secure mode (i.e. a setuid/gid binary using LD_PRELOAD),
536 we don't load the library if it isn't setuid. */
537 if (rflags
& __RTLD_SECURE
) {
538 if (!(st
.st_mode
& S_ISUID
)) {
544 /* Check if file is already loaded */
545 for (tpnt
= _dl_loaded_modules
; tpnt
; tpnt
= tpnt
->next
) {
546 if (tpnt
->st_dev
== st
.st_dev
&& tpnt
->st_ino
== st
.st_ino
) {
549 tpnt
->init_flag
|= DL_OPENED2
;
554 if (rflags
& RTLD_NOLOAD
) {
558 header
= _dl_mmap((void *) 0, _dl_pagesize
, PROT_READ
| PROT_WRITE
,
559 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_UNINITIALIZED
, -1, 0);
560 if (_dl_mmap_check_error(header
)) {
561 _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname
, __LINE__
, libname
);
562 _dl_internal_error_number
= LD_ERROR_MMAP_FAILED
;
567 _dl_read(infile
, header
, _dl_pagesize
);
568 epnt
= (ElfW(Ehdr
) *) (intptr_t) header
;
569 p32
= (uint32_t*)&epnt
->e_ident
;
570 if (*p32
!= ELFMAG_U32
) {
571 _dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname
,
573 _dl_internal_error_number
= LD_ERROR_NOTELF
;
575 _dl_munmap(header
, _dl_pagesize
);
579 if ((epnt
->e_type
!= ET_DYN
580 #ifdef __LDSO_STANDALONE_SUPPORT__
581 && epnt
->e_type
!= ET_EXEC
583 ) || (epnt
->e_machine
!= MAGIC1
585 && epnt
->e_machine
!= MAGIC2
589 _dl_internal_error_number
=
590 (epnt
->e_type
!= ET_DYN
? LD_ERROR_NOTDYN
: LD_ERROR_NOTMAGIC
);
591 _dl_dprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
592 "\n", _dl_progname
, libname
);
594 _dl_munmap(header
, _dl_pagesize
);
598 ppnt
= (ElfW(Phdr
) *)(intptr_t) & header
[epnt
->e_phoff
];
601 for (i
= 0; i
< epnt
->e_phnum
; i
++) {
603 if (ppnt
->p_type
== PT_DYNAMIC
) {
605 _dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
606 _dl_progname
, libname
);
607 dynamic_addr
= ppnt
->p_vaddr
;
610 if (ppnt
->p_type
== PT_LOAD
) {
611 /* See if this is a PIC library. */
612 if (minvma
== 0xffffffff && ppnt
->p_vaddr
> 0x1000000) {
614 minvma
= ppnt
->p_vaddr
;
616 if (piclib
&& ppnt
->p_vaddr
< minvma
) {
617 minvma
= ppnt
->p_vaddr
;
619 if (((unsigned long) ppnt
->p_vaddr
+ ppnt
->p_memsz
) > maxvma
) {
620 maxvma
= ppnt
->p_vaddr
+ ppnt
->p_memsz
;
623 if (ppnt
->p_type
== PT_TLS
) {
624 #if defined(USE_TLS) && USE_TLS
625 if (ppnt
->p_memsz
== 0)
626 /* Nothing to do for an empty segment. */
629 /* Save for after 'tpnt' is actually allocated. */
633 * Yup, the user was an idiot and tried to sneak in a library with
634 * TLS in it and we don't support it. Let's fall on our own sword
635 * and scream at the luser while we die.
637 _dl_dprintf(2, "%s: '%s' library contains unsupported TLS\n",
638 _dl_progname
, libname
);
639 _dl_internal_error_number
= LD_ERROR_TLS_FAILED
;
641 _dl_munmap(header
, _dl_pagesize
);
648 #ifdef __LDSO_STANDALONE_SUPPORT__
649 if (epnt
->e_type
== ET_EXEC
)
653 DL_CHECK_LIB_TYPE (epnt
, piclib
, _dl_progname
, libname
);
655 maxvma
= (maxvma
+ ADDR_ALIGN
) & PAGE_ALIGN
;
656 minvma
= minvma
& ~ADDR_ALIGN
;
658 flags
= MAP_PRIVATE
/*| MAP_DENYWRITE */ ;
660 if (piclib
== 0 || piclib
== 1) {
661 status
= (char *) _dl_mmap((char *) (piclib
? 0 : minvma
),
662 maxvma
- minvma
, PROT_NONE
, flags
| MAP_ANONYMOUS
, -1, 0);
663 if (_dl_mmap_check_error(status
)) {
665 _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname
, __LINE__
, libname
);
666 _dl_internal_error_number
= LD_ERROR_MMAP_FAILED
;
668 _dl_munmap(header
, _dl_pagesize
);
671 libaddr
= (unsigned long) status
;
675 /* Get the memory to store the library */
676 ppnt
= (ElfW(Phdr
) *)(intptr_t) & header
[epnt
->e_phoff
];
678 DL_INIT_LOADADDR(lib_loadaddr
, libaddr
- minvma
, ppnt
, epnt
->e_phnum
);
679 /* Set _dl_library_offset to lib_loadaddr or 0. */
680 DL_SET_LIB_OFFSET(lib_loadaddr
);
682 for (i
= 0; i
< epnt
->e_phnum
; i
++) {
683 if (DL_IS_SPECIAL_SEGMENT (epnt
, ppnt
)) {
686 addr
= DL_MAP_SEGMENT (epnt
, ppnt
, infile
, flags
);
689 DL_LOADADDR_UNMAP (lib_loadaddr
, maxvma
- minvma
);
693 DL_INIT_LOADADDR_HDR (lib_loadaddr
, addr
, ppnt
);
697 if (ppnt
->p_type
== PT_GNU_RELRO
) {
698 relro_addr
= ppnt
->p_vaddr
;
699 relro_size
= ppnt
->p_memsz
;
701 if (ppnt
->p_type
== PT_LOAD
) {
705 if (ppnt
->p_flags
& PF_W
) {
706 status
= map_writeable (infile
, ppnt
, piclib
, flags
, libaddr
);
710 tryaddr
= (piclib
== 2 ? 0
711 : (char *) (ppnt
->p_vaddr
& PAGE_ALIGN
)
712 + (piclib
? libaddr
: DL_GET_LIB_OFFSET()));
713 size
= (ppnt
->p_vaddr
& ADDR_ALIGN
) + ppnt
->p_filesz
;
714 status
= (char *) _dl_mmap
715 (tryaddr
, size
, LXFLAGS(ppnt
->p_flags
),
716 flags
| (piclib
== 2 ? MAP_EXECUTABLE
717 | MAP_DENYWRITE
: 0),
718 infile
, ppnt
->p_offset
& OFFS_ALIGN
);
719 if (_dl_mmap_check_error(status
)
720 || (tryaddr
&& tryaddr
!= status
))
723 DL_INIT_LOADADDR_HDR(lib_loadaddr
,
724 status
+ (ppnt
->p_vaddr
& ADDR_ALIGN
),
727 /* if (libaddr == 0 && piclib) {
728 libaddr = (unsigned long) status;
736 * The dynamic_addr must be take into acount lib_loadaddr value, to note
737 * it is zero when the SO has been mapped to the elf's physical addr
739 #ifdef __LDSO_PRELINK_SUPPORT__
740 if (DL_GET_LIB_OFFSET()) {
744 dynamic_addr
= (unsigned long) DL_RELOC_ADDR(lib_loadaddr
, dynamic_addr
);
748 * OK, the ELF library is now loaded into VM in the correct locations
749 * The next step is to go through and do the dynamic linking (if needed).
752 /* Start by scanning the dynamic section to get all of the pointers */
755 _dl_internal_error_number
= LD_ERROR_NODYNAMIC
;
756 _dl_dprintf(2, "%s: '%s' is missing a dynamic section\n",
757 _dl_progname
, libname
);
758 _dl_munmap(header
, _dl_pagesize
);
763 dpnt
= (ElfW(Dyn
) *) dynamic_addr
;
764 _dl_memset(dynamic_info
, 0, sizeof(dynamic_info
));
765 rtld_flags
= _dl_parse_dynamic_info(dpnt
, dynamic_info
, NULL
, lib_loadaddr
);
766 /* If the TEXTREL is set, this means that we need to make the pages
767 writable before we perform relocations. Do this now. They get set
770 if (dynamic_info
[DT_TEXTREL
]) {
771 #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
772 ppnt
= (ElfW(Phdr
) *)(intptr_t) & header
[epnt
->e_phoff
];
773 for (i
= 0; i
< epnt
->e_phnum
; i
++, ppnt
++) {
774 if (ppnt
->p_type
== PT_LOAD
&& !(ppnt
->p_flags
& PF_W
)) {
775 #ifdef __ARCH_USE_MMU__
776 _dl_mprotect((void *) ((piclib
? libaddr
: DL_GET_LIB_OFFSET()) +
777 (ppnt
->p_vaddr
& PAGE_ALIGN
)),
778 (ppnt
->p_vaddr
& ADDR_ALIGN
) + (unsigned long) ppnt
->p_filesz
,
779 PROT_READ
| PROT_WRITE
| PROT_EXEC
);
782 new_addr
= map_writeable (infile
, ppnt
, piclib
, flags
, libaddr
);
784 _dl_dprintf(2, "Can't modify %s's text section.",
788 DL_UPDATE_LOADADDR_HDR(lib_loadaddr
,
789 new_addr
+ (ppnt
->p_vaddr
& ADDR_ALIGN
),
791 /* This has invalidated all pointers into the previously readonly segment.
792 Update any them to point into the remapped segment. */
793 _dl_parse_dynamic_info(dpnt
, dynamic_info
, NULL
, lib_loadaddr
);
798 _dl_dprintf(2, "Can't modify %s's text section."
799 " Use GCC option -fPIC for shared objects, please.\n",
807 tpnt
= _dl_add_elf_hash_table(libname
, lib_loadaddr
, dynamic_info
,
809 tpnt
->mapaddr
= libaddr
;
810 tpnt
->relro_addr
= relro_addr
;
811 tpnt
->relro_size
= relro_size
;
812 tpnt
->st_dev
= st
.st_dev
;
813 tpnt
->st_ino
= st
.st_ino
;
814 tpnt
->ppnt
= (ElfW(Phdr
) *)
815 DL_RELOC_ADDR(DL_GET_RUN_ADDR(tpnt
->loadaddr
, tpnt
->mapaddr
),
817 tpnt
->n_phent
= epnt
->e_phnum
;
818 tpnt
->rtld_flags
= rflags
| rtld_flags
;
819 #ifdef __LDSO_STANDALONE_SUPPORT__
820 tpnt
->l_entry
= epnt
->e_entry
;
823 #if defined(USE_TLS) && USE_TLS
825 _dl_debug_early("Found TLS header for %s\n", libname
);
826 # if NO_TLS_OFFSET != 0
827 tpnt
->l_tls_offset
= NO_TLS_OFFSET
;
829 tpnt
->l_tls_blocksize
= tlsppnt
->p_memsz
;
830 tpnt
->l_tls_align
= tlsppnt
->p_align
;
831 if (tlsppnt
->p_align
== 0)
832 tpnt
->l_tls_firstbyte_offset
= 0;
834 tpnt
->l_tls_firstbyte_offset
= tlsppnt
->p_vaddr
&
835 (tlsppnt
->p_align
- 1);
836 tpnt
->l_tls_initimage_size
= tlsppnt
->p_filesz
;
837 tpnt
->l_tls_initimage
= (void *) tlsppnt
->p_vaddr
;
839 /* Assign the next available module ID. */
840 tpnt
->l_tls_modid
= _dl_next_tls_modid ();
842 /* We know the load address, so add it to the offset. */
843 #ifdef __LDSO_STANDALONE_SUPPORT__
844 if ((tpnt
->l_tls_initimage
!= NULL
) && piclib
)
846 if (tpnt
->l_tls_initimage
!= NULL
)
849 # ifdef __SUPPORT_LD_DEBUG_EARLY__
850 char *tmp
= (char *) tpnt
->l_tls_initimage
;
851 tpnt
->l_tls_initimage
= (char *) tlsppnt
->p_vaddr
+ tpnt
->loadaddr
;
852 _dl_debug_early("Relocated TLS initial image from %x to %x (size = %x)\n", tmp
, tpnt
->l_tls_initimage
, tpnt
->l_tls_initimage_size
);
855 tpnt
->l_tls_initimage
= (char *) tlsppnt
->p_vaddr
+ tpnt
->loadaddr
;
862 * Add this object into the symbol chain
865 #ifdef __LDSO_STANDALONE_SUPPORT__
866 /* Do not create a new chain entry for the main executable */
870 (*rpnt
)->next
= _dl_malloc(sizeof(struct dyn_elf
));
871 _dl_memset((*rpnt
)->next
, 0, sizeof(struct dyn_elf
));
872 (*rpnt
)->next
->prev
= (*rpnt
);
873 *rpnt
= (*rpnt
)->next
;
876 /* When statically linked, the first time we dlopen a DSO
877 * the *rpnt is NULL, so we need to allocate memory for it,
878 * and initialize the _dl_symbol_table.
881 *rpnt
= _dl_symbol_tables
= _dl_malloc(sizeof(struct dyn_elf
));
882 _dl_memset(*rpnt
, 0, sizeof(struct dyn_elf
));
887 #ifdef __LDSO_STANDALONE_SUPPORT__
888 tpnt
->libtype
= (epnt
->e_type
== ET_DYN
) ? elf_lib
: elf_executable
;
890 tpnt
->libtype
= elf_lib
;
894 * OK, the next thing we need to do is to insert the dynamic linker into
895 * the proper entry in the GOT so that the PLT symbols can be properly
899 lpnt
= (unsigned long *) dynamic_info
[DT_PLTGOT
];
902 lpnt
= (unsigned long *) (dynamic_info
[DT_PLTGOT
]);
903 INIT_GOT(lpnt
, tpnt
);
907 /* Handle DSBT initialization */
909 struct elf_resolve
*t
, *ref
;
910 int idx
= tpnt
->dsbt_index
;
911 void **dsbt
= tpnt
->dsbt_table
;
914 * It is okay (required actually) to have zero idx for an executable.
915 * This is the case when running ldso standalone and the program
916 * is being mapped in via _dl_load_shared_library().
918 if (idx
== 0 && tpnt
->libtype
!= elf_executable
) {
919 if (!dynamic_info
[DT_TEXTREL
]) {
920 /* This DSO has not been assigned an index. */
921 _dl_dprintf(2, "%s: '%s' is missing a dsbt index assignment!\n",
922 _dl_progname
, libname
);
925 /* Find a dsbt table from another module. */
927 for (t
= _dl_loaded_modules
; t
; t
= t
->next
) {
928 if (ref
== NULL
&& t
!= tpnt
) {
933 idx
= tpnt
->dsbt_size
;
935 if (!ref
|| ref
->dsbt_table
[idx
] == NULL
)
938 _dl_dprintf(2, "%s: '%s' caused DSBT table overflow!\n",
939 _dl_progname
, libname
);
942 _dl_if_debug_dprint("\n\tfile='%s'; assigned index %d\n",
944 tpnt
->dsbt_index
= idx
;
947 /* make sure index is not already used */
948 if (_dl_ldso_dsbt
[idx
]) {
949 struct elf_resolve
*dup
;
950 const char *dup_name
;
952 for (dup
= _dl_loaded_modules
; dup
; dup
= dup
->next
)
953 if (dup
!= tpnt
&& dup
->dsbt_index
== idx
)
956 dup_name
= dup
->libname
;
958 dup_name
= "runtime linker";
960 dup_name
= "unknown library";
961 _dl_dprintf(2, "%s: '%s' dsbt index %d already used by %s!\n",
962 _dl_progname
, libname
, idx
, dup_name
);
967 * Setup dsbt slot for this module in dsbt of all modules.
969 for (t
= _dl_loaded_modules
; t
; t
= t
->next
)
970 t
->dsbt_table
[idx
] = dsbt
;
971 _dl_ldso_dsbt
[idx
] = dsbt
;
972 _dl_memcpy(dsbt
, _dl_ldso_dsbt
,
973 tpnt
->dsbt_size
* sizeof(tpnt
->dsbt_table
[0]));
976 _dl_if_debug_dprint("\n\tfile='%s'; generating link map\n", libname
);
977 _dl_if_debug_dprint("\t\tdynamic: %x base: %x\n", dynamic_addr
, DL_LOADADDR_BASE(lib_loadaddr
));
978 _dl_if_debug_dprint("\t\t entry: %x phdr: %x phnum: %x\n\n",
979 DL_RELOC_ADDR(lib_loadaddr
, epnt
->e_entry
), tpnt
->ppnt
, tpnt
->n_phent
);
981 _dl_munmap(header
, _dl_pagesize
);
986 /* now_flag must be RTLD_NOW or zero */
987 int _dl_fixup(struct dyn_elf
*rpnt
, struct r_scope_elem
*scope
, int now_flag
)
990 struct elf_resolve
*tpnt
;
991 ElfW(Word
) reloc_size
, relative_count
;
992 ElfW(Addr
) reloc_addr
;
995 goof
= _dl_fixup(rpnt
->next
, scope
, now_flag
);
1000 if (!(tpnt
->init_flag
& RELOCS_DONE
))
1001 _dl_if_debug_dprint("relocation processing: %s\n", tpnt
->libname
);
1003 if (unlikely(tpnt
->dynamic_info
[UNSUPPORTED_RELOC_TYPE
])) {
1004 _dl_if_debug_dprint("%s: can't handle %s relocation records\n",
1005 _dl_progname
, UNSUPPORTED_RELOC_STR
);
1010 reloc_size
= tpnt
->dynamic_info
[DT_RELOC_TABLE_SIZE
];
1011 /* On some machines, notably SPARC & PPC, DT_REL* includes DT_JMPREL in its
1012 range. Note that according to the ELF spec, this is completely legal! */
1013 #ifdef ELF_MACHINE_PLTREL_OVERLAP
1014 reloc_size
-= tpnt
->dynamic_info
[DT_PLTRELSZ
];
1016 if (tpnt
->dynamic_info
[DT_RELOC_TABLE_ADDR
] &&
1017 !(tpnt
->init_flag
& RELOCS_DONE
)) {
1018 reloc_addr
= tpnt
->dynamic_info
[DT_RELOC_TABLE_ADDR
];
1019 relative_count
= tpnt
->dynamic_info
[DT_RELCONT_IDX
];
1020 if (relative_count
) { /* Optimize the XX_RELATIVE relocations if possible */
1021 reloc_size
-= relative_count
* sizeof(ELF_RELOC
);
1022 #ifdef __LDSO_PRELINK_SUPPORT__
1023 if (tpnt
->loadaddr
|| (!tpnt
->dynamic_info
[DT_GNU_PRELINKED_IDX
]))
1025 elf_machine_relative(tpnt
->loadaddr
, reloc_addr
, relative_count
);
1026 reloc_addr
+= relative_count
* sizeof(ELF_RELOC
);
1028 goof
+= _dl_parse_relocation_information(rpnt
, scope
,
1031 tpnt
->init_flag
|= RELOCS_DONE
;
1033 if (tpnt
->dynamic_info
[DT_BIND_NOW
])
1034 now_flag
= RTLD_NOW
;
1035 if (tpnt
->dynamic_info
[DT_JMPREL
] &&
1036 (!(tpnt
->init_flag
& JMP_RELOCS_DONE
) ||
1037 (now_flag
&& !(tpnt
->rtld_flags
& now_flag
)))) {
1038 tpnt
->rtld_flags
|= now_flag
;
1039 if (!(tpnt
->rtld_flags
& RTLD_NOW
)) {
1040 _dl_parse_lazy_relocation_information(rpnt
,
1041 tpnt
->dynamic_info
[DT_JMPREL
],
1042 tpnt
->dynamic_info
[DT_PLTRELSZ
]);
1044 goof
+= _dl_parse_relocation_information(rpnt
, scope
,
1045 tpnt
->dynamic_info
[DT_JMPREL
],
1046 tpnt
->dynamic_info
[DT_PLTRELSZ
]);
1048 tpnt
->init_flag
|= JMP_RELOCS_DONE
;
1052 /* _dl_add_to_slotinfo is called by init_tls() for initial DSO
1053 or by dlopen() for dynamically loaded DSO. */
1054 #if defined(USE_TLS) && USE_TLS
1055 /* Add object to slot information data if necessasy. */
1056 if (tpnt
->l_tls_blocksize
!= 0 && tls_init_tp_called
)
1057 _dl_add_to_slotinfo ((struct link_map
*) tpnt
);
1064 /* Minimal printf which handles only %s, %d, and %x */
1065 void _dl_dprintf(int fd
, const char *fmt
, ...)
1073 char *start
, *ptr
, *string
;
1079 buf
= _dl_mmap((void *) 0, _dl_pagesize
, PROT_READ
| PROT_WRITE
,
1080 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
1081 if (_dl_mmap_check_error(buf
)) {
1082 _dl_write(fd
, "mmap of a spare page failed!\n", 29);
1088 if (_dl_strlen(fmt
) >= (_dl_pagesize
- 1)) {
1089 _dl_write(fd
, "overflow\n", 11);
1093 _dl_strcpy(buf
, fmt
);
1094 va_start(args
, fmt
);
1097 while (*ptr
!= '%' && *ptr
) {
1103 _dl_write(fd
, start
, _dl_strlen(start
));
1107 string
= va_arg(args
, char *);
1110 _dl_write(fd
, "(null)", 6);
1112 _dl_write(fd
, string
, _dl_strlen(string
));
1120 num
= va_arg(args
, long int);
1122 num
= va_arg(args
, int);
1124 string
= _dl_simple_ltoa(tmp
, num
);
1125 _dl_write(fd
, string
, _dl_strlen(string
));
1133 num
= va_arg(args
, long int);
1135 num
= va_arg(args
, int);
1137 string
= _dl_simple_ltoahex(tmp
, num
);
1138 _dl_write(fd
, string
, _dl_strlen(string
));
1142 _dl_write(fd
, "(null)", 6);
1148 _dl_write(fd
, start
, _dl_strlen(start
));
1152 _dl_munmap(buf
, _dl_pagesize
);
1156 char *_dl_strdup(const char *string
)
1161 len
= _dl_strlen(string
);
1162 retval
= _dl_malloc(len
+ 1);
1163 _dl_strcpy(retval
, string
);
1168 unsigned int _dl_parse_dynamic_info(ElfW(Dyn
) *dpnt
, unsigned long dynamic_info
[],
1169 void *debug_addr
, DL_LOADADDR_TYPE load_off
)
1171 return __dl_parse_dynamic_info(dpnt
, dynamic_info
, debug_addr
, load_off
);