ldso: Consistently set & use DL_OPENED flag in both ld.so and libdl
[uclibc-ng.git] / ldso / ldso / dl-elf.c
blob8f71aeb055a666bda98adf648647dddbf5087706
1 /* vi: set sw=4 ts=4: */
2 /*
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
12 * are met:
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
29 * SUCH DAMAGE.
33 #include "ldso.h"
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)
42 int fd;
43 struct stat st;
44 header_t *header;
45 libentry_t *libent;
46 int i, strtabsize;
48 if (_dl_cache_addr == MAP_FAILED)
49 return -1;
50 else if (_dl_cache_addr != NULL)
51 return 0;
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 */
56 return -1;
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);
61 _dl_close(fd);
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);
65 return -1;
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)
73 || _dl_cache_size <
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,
78 LDSO_CACHE);
79 goto fail;
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);
91 goto fail;
95 return 0;
97 fail:
98 _dl_munmap(_dl_cache_addr, _dl_cache_size);
99 _dl_cache_addr = MAP_FAILED;
100 return -1;
103 int _dl_unmap_cache(void)
105 if (_dl_cache_addr == NULL || _dl_cache_addr == MAP_FAILED)
106 return -1;
108 #if 1
109 _dl_munmap(_dl_cache_addr, _dl_cache_size);
110 _dl_cache_addr = NULL;
111 #endif
113 return 0;
115 #endif
118 void
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);
125 if (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);
128 _dl_exit(0);
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)
138 char *mylibname;
139 struct elf_resolve *tpnt;
140 const char *p, *pn;
141 int plen;
143 if (path_list==NULL)
144 return NULL;
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 */
154 /* "" maps to CWD */
155 for (p = path_list; p != NULL; p = pn) {
156 pn = _dl_strchr(p + 1, ':');
157 if (pn != NULL) {
158 plen = pn - p;
159 pn++;
160 } else
161 plen = _dl_strlen(p);
163 if (plen >= 7 && _dl_memcmp(p, "$ORIGIN", 7) == 0) {
164 int olen;
165 /* $ORIGIN is not expanded for SUID/GUID programs
166 (except if it is $ORIGIN alone) */
167 if ((rflags & __RTLD_SECURE) && plen != 7)
168 continue;
169 if (origin == NULL)
170 continue;
171 for (olen = _dl_strlen(origin) - 1; olen >= 0 && origin[olen] != '/'; olen--)
173 if (olen <= 0)
174 continue;
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);
180 mylibname[plen] = 0;
181 } else {
182 _dl_strcpy(mylibname, ".");
184 _dl_strcat(mylibname, "/");
185 _dl_strcat(mylibname, name);
186 #ifdef __LDSO_SAFE_RUNPATH__
187 if (*mylibname == '/')
188 #endif
189 if ((tpnt = _dl_load_elf_shared_library(rflags, rpnt, mylibname)) != NULL)
190 return tpnt;
192 return 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)
202 char *pnt;
203 struct elf_resolve *tpnt1;
204 char *libname;
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)
212 goto goof;
214 /* Skip over any initial initial './' and '/' stuff to
215 * get the short form libname with no path garbage */
216 pnt = _dl_strrchr(libname, '/');
217 if (pnt) {
218 libname = pnt + 1;
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);
229 if (tpnt1) {
230 return tpnt1;
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);
240 if (pnt) {
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)
245 return tpnt1;
248 #endif
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)
256 return tpnt1;
259 #endif
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);
265 if (pnt) {
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)
269 return tpnt1;
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];
276 if (pnt) {
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)
280 return tpnt1;
282 #endif
283 #endif
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) {
292 int i;
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))
305 return tpnt1;
309 #endif
310 #if defined SHARED && defined __LDSO_SEARCH_INTERP_PATH__
311 /* Look for libraries wherever the shared library loader
312 * was installed */
313 _dl_if_debug_dprint("\tsearching ldso dir='%s'\n", _dl_ldsopath);
314 tpnt1 = search_for_named_library(libname, rflags, _dl_ldsopath, rpnt, NULL);
315 if (tpnt1 != NULL)
316 return tpnt1;
317 #endif
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"
326 #endif
327 , rpnt, NULL);
328 if (tpnt1 != NULL)
329 return tpnt1;
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];
347 if (pnt) {
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)
351 return tpnt1;
353 pnt = (char *) _dl_loaded_modules->dynamic_info[DT_RPATH];
354 if (pnt) {
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)
358 return tpnt1;
360 #endif
363 goof:
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;
367 else
368 _dl_error_number = LD_ERROR_NOFILE;
369 _dl_if_debug_dprint("Bummer: could not find '%s'!\n", libname);
370 return NULL;
373 /* Define the _dl_library_offset for the architectures that need it */
374 DL_DEF_LIB_OFFSET;
377 * Make a writeable mapping of a segment, regardless of whether PF_W is
378 * set or not.
380 static void *
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;
386 char *tryaddr;
387 ssize_t size;
388 unsigned long map_size;
389 char *cpnt;
390 char *piclib2map = NULL;
392 if (piclib == 2 &&
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
399 calls.
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))
414 return 0;
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
426 to mmap (). */
427 #ifndef __ARCH_USE_MMU__
428 if (piclib2map == 0)
429 #endif
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__
435 else
436 status = MAP_FAILED;
437 #endif
438 #ifdef _DL_PREAD
439 if (_dl_mmap_check_error(status) && piclib2map
440 && (_DL_PREAD (infile, tryaddr, size,
441 ppnt->p_offset & OFFS_ALIGN) == size))
442 status = tryaddr;
443 #endif
444 if (_dl_mmap_check_error(status) || (tryaddr && tryaddr != status))
445 return 0;
447 if (piclib2map)
448 retval = piclib2map;
449 else
450 retval = 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
465 filesz. */
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;
473 _dl_memset (cpnt, 0,
474 MIN (map_size
475 - (ppnt->p_vaddr
476 + ppnt->p_filesz),
477 ppnt->p_memsz
478 - ppnt->p_filesz));
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,
484 LXFLAGS(prot_flags),
485 flags | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
486 if (_dl_mmap_check_error(status) || tryaddr != status)
487 return NULL;
489 return retval;
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
495 * are required.
498 struct elf_resolve *_dl_load_elf_shared_library(unsigned int rflags,
499 struct dyn_elf **rpnt, const char *libname)
501 ElfW(Ehdr) *epnt;
502 unsigned long dynamic_addr = 0;
503 ElfW(Dyn) *dpnt;
504 struct elf_resolve *tpnt;
505 ElfW(Phdr) *ppnt;
506 #if defined(USE_TLS) && USE_TLS
507 ElfW(Phdr) *tlsppnt = NULL;
508 #endif
509 char *status, *header;
510 unsigned long dynamic_info[DYNAMIC_SIZE];
511 unsigned long *lpnt;
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;
518 struct stat st;
519 uint32_t *p32;
520 DL_LOADADDR_TYPE lib_loadaddr;
521 DL_INIT_LOADADDR_EXTRA_DECLS
523 libaddr = 0;
524 infile = _dl_open(libname, O_RDONLY, 0);
525 if (infile < 0) {
526 _dl_internal_error_number = LD_ERROR_NOFILE;
527 return NULL;
530 if (_dl_fstat(infile, &st) < 0) {
531 _dl_internal_error_number = LD_ERROR_NOFILE;
532 _dl_close(infile);
533 return NULL;
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)) {
539 _dl_close(infile);
540 return NULL;
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) {
547 /* Already loaded */
548 tpnt->usage_count++;
549 tpnt->init_flag |= DL_OPENED2;
550 _dl_close(infile);
551 return tpnt;
554 if (rflags & RTLD_NOLOAD) {
555 _dl_close(infile);
556 return NULL;
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;
563 _dl_close(infile);
564 return NULL;
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,
572 libname);
573 _dl_internal_error_number = LD_ERROR_NOTELF;
574 _dl_close(infile);
575 _dl_munmap(header, _dl_pagesize);
576 return NULL;
579 if ((epnt->e_type != ET_DYN
580 #ifdef __LDSO_STANDALONE_SUPPORT__
581 && epnt->e_type != ET_EXEC
582 #endif
583 ) || (epnt->e_machine != MAGIC1
584 #ifdef MAGIC2
585 && epnt->e_machine != MAGIC2
586 #endif
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);
593 _dl_close(infile);
594 _dl_munmap(header, _dl_pagesize);
595 return NULL;
598 ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
600 piclib = 1;
601 for (i = 0; i < epnt->e_phnum; i++) {
603 if (ppnt->p_type == PT_DYNAMIC) {
604 if (dynamic_addr)
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) {
613 piclib = 0;
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. */
627 continue;
628 else
629 /* Save for after 'tpnt' is actually allocated. */
630 tlsppnt = ppnt;
631 #else
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;
640 _dl_close(infile);
641 _dl_munmap(header, _dl_pagesize);
642 return NULL;
643 #endif
645 ppnt++;
648 #ifdef __LDSO_STANDALONE_SUPPORT__
649 if (epnt->e_type == ET_EXEC)
650 piclib = 0;
651 #endif
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)) {
664 cant_map:
665 _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
666 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
667 _dl_close(infile);
668 _dl_munmap(header, _dl_pagesize);
669 return NULL;
671 libaddr = (unsigned long) status;
672 flags |= MAP_FIXED;
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)) {
684 char *addr;
686 addr = DL_MAP_SEGMENT (epnt, ppnt, infile, flags);
687 if (addr == NULL) {
688 cant_map1:
689 DL_LOADADDR_UNMAP (lib_loadaddr, maxvma - minvma);
690 goto cant_map;
693 DL_INIT_LOADADDR_HDR (lib_loadaddr, addr, ppnt);
694 ppnt++;
695 continue;
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) {
702 char *tryaddr;
703 ssize_t size;
705 if (ppnt->p_flags & PF_W) {
706 status = map_writeable (infile, ppnt, piclib, flags, libaddr);
707 if (status == NULL)
708 goto cant_map1;
709 } else {
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))
721 goto cant_map1;
723 DL_INIT_LOADADDR_HDR(lib_loadaddr,
724 status + (ppnt->p_vaddr & ADDR_ALIGN),
725 ppnt);
727 /* if (libaddr == 0 && piclib) {
728 libaddr = (unsigned long) status;
729 flags |= MAP_FIXED;
730 } */
732 ppnt++;
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()) {
741 #else
742 if (piclib) {
743 #endif
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 */
754 if (!dynamic_addr) {
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);
759 _dl_close(infile);
760 return NULL;
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
768 back again later. */
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);
780 #else
781 void *new_addr;
782 new_addr = map_writeable (infile, ppnt, piclib, flags, libaddr);
783 if (!new_addr) {
784 _dl_dprintf(2, "Can't modify %s's text section.",
785 libname);
786 _dl_exit(1);
788 DL_UPDATE_LOADADDR_HDR(lib_loadaddr,
789 new_addr + (ppnt->p_vaddr & ADDR_ALIGN),
790 ppnt);
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);
794 #endif
797 #else
798 _dl_dprintf(2, "Can't modify %s's text section."
799 " Use GCC option -fPIC for shared objects, please.\n",
800 libname);
801 _dl_exit(1);
802 #endif
805 _dl_close(infile);
807 tpnt = _dl_add_elf_hash_table(libname, lib_loadaddr, dynamic_info,
808 dynamic_addr, 0);
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),
816 epnt->e_phoff);
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;
821 #endif
823 #if defined(USE_TLS) && USE_TLS
824 if (tlsppnt) {
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;
828 # endif
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;
833 else
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)
845 #else
846 if (tpnt->l_tls_initimage != NULL)
847 #endif
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);
853 tmp = 0;
854 # else
855 tpnt->l_tls_initimage = (char *) tlsppnt->p_vaddr + tpnt->loadaddr;
856 # endif
859 #endif
862 * Add this object into the symbol chain
864 if (*rpnt
865 #ifdef __LDSO_STANDALONE_SUPPORT__
866 /* Do not create a new chain entry for the main executable */
867 && (*rpnt)->dyn
868 #endif
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;
875 #ifndef SHARED
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.
880 else {
881 *rpnt = _dl_symbol_tables = _dl_malloc(sizeof(struct dyn_elf));
882 _dl_memset(*rpnt, 0, sizeof(struct dyn_elf));
884 #endif
885 (*rpnt)->dyn = tpnt;
886 tpnt->usage_count++;
887 #ifdef __LDSO_STANDALONE_SUPPORT__
888 tpnt->libtype = (epnt->e_type == ET_DYN) ? elf_lib : elf_executable;
889 #else
890 tpnt->libtype = elf_lib;
891 #endif
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
896 * resolved.
899 lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
901 if (lpnt) {
902 lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT]);
903 INIT_GOT(lpnt, tpnt);
906 #ifdef __DSBT__
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);
923 _dl_exit(1);
925 /* Find a dsbt table from another module. */
926 ref = NULL;
927 for (t = _dl_loaded_modules; t; t = t->next) {
928 if (ref == NULL && t != tpnt) {
929 ref = t;
930 break;
933 idx = tpnt->dsbt_size;
934 while (idx-- > 0)
935 if (!ref || ref->dsbt_table[idx] == NULL)
936 break;
937 if (idx <= 0) {
938 _dl_dprintf(2, "%s: '%s' caused DSBT table overflow!\n",
939 _dl_progname, libname);
940 _dl_exit(1);
942 _dl_if_debug_dprint("\n\tfile='%s'; assigned index %d\n",
943 libname, idx);
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)
954 break;
955 if (dup)
956 dup_name = dup->libname;
957 else if (idx == 1)
958 dup_name = "runtime linker";
959 else
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);
963 _dl_exit(1);
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]));
975 #endif
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);
983 return tpnt;
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)
989 int goof = 0;
990 struct elf_resolve *tpnt;
991 ElfW(Word) reloc_size, relative_count;
992 ElfW(Addr) reloc_addr;
994 if (rpnt->next)
995 goof = _dl_fixup(rpnt->next, scope, now_flag);
996 if (goof)
997 return goof;
998 tpnt = rpnt->dyn;
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);
1006 goof++;
1007 return goof;
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];
1015 #endif
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]))
1024 #endif
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,
1029 reloc_addr,
1030 reloc_size);
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]);
1043 } else {
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;
1051 #if 0
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);
1058 #endif
1059 #endif
1060 return goof;
1063 #ifdef IS_IN_rtld
1064 /* Minimal printf which handles only %s, %d, and %x */
1065 void _dl_dprintf(int fd, const char *fmt, ...)
1067 #if __WORDSIZE > 32
1068 long int num;
1069 #else
1070 int num;
1071 #endif
1072 va_list args;
1073 char *start, *ptr, *string;
1074 char *buf;
1076 if (!fmt)
1077 return;
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);
1083 _dl_exit(20);
1086 start = ptr = buf;
1088 if (_dl_strlen(fmt) >= (_dl_pagesize - 1)) {
1089 _dl_write(fd, "overflow\n", 11);
1090 _dl_exit(20);
1093 _dl_strcpy(buf, fmt);
1094 va_start(args, fmt);
1096 while (start) {
1097 while (*ptr != '%' && *ptr) {
1098 ptr++;
1101 if (*ptr == '%') {
1102 *ptr++ = '\0';
1103 _dl_write(fd, start, _dl_strlen(start));
1105 switch (*ptr++) {
1106 case 's':
1107 string = va_arg(args, char *);
1109 if (!string)
1110 _dl_write(fd, "(null)", 6);
1111 else
1112 _dl_write(fd, string, _dl_strlen(string));
1113 break;
1115 case 'i':
1116 case 'd':
1118 char tmp[22];
1119 #if __WORDSIZE > 32
1120 num = va_arg(args, long int);
1121 #else
1122 num = va_arg(args, int);
1123 #endif
1124 string = _dl_simple_ltoa(tmp, num);
1125 _dl_write(fd, string, _dl_strlen(string));
1126 break;
1128 case 'x':
1129 case 'p':
1131 char tmp[22];
1132 #if __WORDSIZE > 32
1133 num = va_arg(args, long int);
1134 #else
1135 num = va_arg(args, int);
1136 #endif
1137 string = _dl_simple_ltoahex(tmp, num);
1138 _dl_write(fd, string, _dl_strlen(string));
1139 break;
1141 default:
1142 _dl_write(fd, "(null)", 6);
1143 break;
1146 start = ptr;
1147 } else {
1148 _dl_write(fd, start, _dl_strlen(start));
1149 start = NULL;
1152 _dl_munmap(buf, _dl_pagesize);
1153 return;
1156 char *_dl_strdup(const char *string)
1158 char *retval;
1159 int len;
1161 len = _dl_strlen(string);
1162 retval = _dl_malloc(len + 1);
1163 _dl_strcpy(retval, string);
1164 return retval;
1166 #endif
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);