libdl: fix problem with unmapping
[uclibc-ng.git] / ldso / libdl / libdl.c
blobebf6ae329f5914f986047879ad15c0409d107488
1 /*
2 * Program to load an ELF binary on a linux system, and run it
3 * after resolving ELF shared library symbols
5 * Copyright (C) 2000-2006 by Erik Andersen <andersen@uclibc.org>
6 * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
7 * David Engel, Hongjiu Lu and Mitch D'Souza
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. The name of the above contributors may not be
15 * used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
32 /* When libdl is linked in statically into libc.a, we need to replace
33 * these symbols that otherwise would have been loaded in from ldso.
34 * This must be before including ldso.h */
35 #ifndef SHARED
36 #define _dl_malloc malloc
37 #define _dl_free free
38 #endif
40 #include <ldso.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <stdbool.h>
44 #include <bits/uClibc_mutex.h>
46 #ifdef __UCLIBC_HAS_TLS__
47 #include <tls.h>
48 #endif
50 #if defined(USE_TLS) && USE_TLS
51 #include <ldsodefs.h>
52 #include <dl-tls.h>
53 extern void _dl_add_to_slotinfo(struct link_map *l);
54 #endif
56 /* TODO: get rid of global lock and use more finegrained locking, or
57 * perhaps RCU for the global structures */
58 __UCLIBC_MUTEX_STATIC(_dl_mutex, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
60 #ifdef SHARED
61 # if defined(USE_TLS) && USE_TLS
62 extern struct link_map *_dl_update_slotinfo(unsigned long int req_modid);
63 # endif
65 /* When libdl is loaded as a shared library, we need to load in
66 * and use a pile of symbols from ldso... */
67 #include <dl-elf.h>
68 extern int _dl_errno;
69 extern struct dyn_elf *_dl_symbol_tables;
70 extern struct dyn_elf *_dl_handles;
71 extern struct elf_resolve *_dl_loaded_modules;
72 extern void _dl_free (void *__ptr);
73 extern struct r_debug *_dl_debug_addr;
74 extern unsigned long _dl_error_number;
75 extern void *(*_dl_malloc_function)(size_t);
76 extern void (*_dl_free_function) (void *p);
77 extern void _dl_run_init_array(struct elf_resolve *);
78 extern void _dl_run_fini_array(struct elf_resolve *);
79 #ifdef __LDSO_CACHE_SUPPORT__
80 int _dl_map_cache(void);
81 int _dl_unmap_cache(void);
82 #endif
83 #ifdef __mips__
84 extern void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt, int lazy);
85 #endif
86 #ifdef __SUPPORT_LD_DEBUG__
87 extern char *_dl_debug;
88 #endif
90 #else /* !SHARED */
92 /* When libdl is linked as a static library, we need to replace all
93 * the symbols that otherwise would have been loaded in from ldso... */
95 #ifdef __SUPPORT_LD_DEBUG__
96 char *_dl_debug = NULL;
97 char *_dl_debug_symbols = NULL;
98 char *_dl_debug_move = NULL;
99 char *_dl_debug_reloc = NULL;
100 char *_dl_debug_detail = NULL;
101 char *_dl_debug_nofixups = NULL;
102 char *_dl_debug_bindings = NULL;
103 int _dl_debug_file = 2;
104 #endif
105 const char *_dl_progname = ""; /* Program name */
106 void *(*_dl_malloc_function)(size_t);
107 void (*_dl_free_function) (void *p);
108 #ifdef __LDSO_LD_LIBRARY_PATH__
109 char *_dl_library_path = NULL; /* Where we look for libraries */
110 #endif
111 int _dl_errno = 0; /* We can't use the real errno in ldso */
112 size_t _dl_pagesize = PAGE_SIZE; /* Store the page size for use later */
113 /* This global variable is also to communicate with debuggers such as gdb. */
114 struct r_debug *_dl_debug_addr = NULL;
116 #include "../ldso/dl-array.c"
117 #include "../ldso/dl-debug.c"
119 # if defined(USE_TLS) && USE_TLS
121 * Giving this initialized value preallocates some surplus bytes in the
122 * static TLS area, see __libc_setup_tls (libc-tls.c).
124 size_t _dl_tls_static_size = 2048;
125 # endif
126 #include LDSO_ELFINTERP
127 #include "../ldso/dl-hash.c"
128 #define _dl_trace_loaded_objects 0
129 #include "../ldso/dl-elf.c"
130 #endif /* SHARED */
132 #ifdef __SUPPORT_LD_DEBUG__
133 # define _dl_if_debug_print(fmt, args...) \
134 do { \
135 if (_dl_debug) \
136 fprintf(stderr, "%s():%i: " fmt, __func__, __LINE__, ## args); \
137 } while (0)
138 #else
139 # define _dl_if_debug_print(fmt, args...)
140 #endif
142 static int do_dlclose(void *, int need_fini);
145 static const char *const dl_error_names[] = {
147 "File not found",
148 "Unable to open /dev/zero",
149 "Not an ELF file",
150 #if defined (__i386__)
151 "Not i386 binary",
152 #elif defined (__sparc__)
153 "Not sparc binary",
154 #elif defined (__mc68000__)
155 "Not m68k binary",
156 #else
157 "Unrecognized binary type",
158 #endif
159 "Not an ELF shared library",
160 "Unable to mmap file",
161 "No dynamic section",
162 "Library contains unsupported TLS",
163 #ifdef ELF_USES_RELOCA
164 "Unable to process REL relocs",
165 #else
166 "Unable to process RELA relocs",
167 #endif
168 "Bad handle",
169 "Unable to resolve symbol"
173 #if defined(USE_TLS) && USE_TLS
174 #ifdef SHARED
176 * Systems which do not have tls_index also probably have to define
177 * DONT_USE_TLS_INDEX.
180 # ifndef __TLS_GET_ADDR
181 # define __TLS_GET_ADDR __tls_get_addr
182 # endif
185 * Return the symbol address given the map of the module it is in and
186 * the symbol record. This is used in dl-sym.c.
188 static void *
189 internal_function
190 _dl_tls_symaddr(struct link_map *map, const Elf32_Addr st_value)
192 # ifndef DONT_USE_TLS_INDEX
193 tls_index tmp =
195 .ti_module = map->l_tls_modid,
196 .ti_offset = st_value
199 return __TLS_GET_ADDR (&tmp);
200 # else
201 return __TLS_GET_ADDR (map->l_tls_modid, st_value);
202 # endif
204 #endif
206 /* Returns true when a non-empty entry was found. */
207 static bool
208 remove_slotinfo(size_t idx, struct dtv_slotinfo_list *listp, size_t disp,
209 bool should_be_there)
211 if (idx - disp >= listp->len) {
212 if (listp->next == NULL) {
214 * The index is not actually valid in the slotinfo list,
215 * because this object was closed before it was fully set
216 * up due to some error.
218 _dl_assert(!should_be_there);
219 } else {
220 if (remove_slotinfo(idx, listp->next, disp + listp->len,
221 should_be_there))
222 return true;
225 * No non-empty entry. Search from the end of this element's
226 * slotinfo array.
228 idx = disp + listp->len;
230 } else {
231 struct link_map *old_map = listp->slotinfo[idx - disp].map;
234 * The entry might still be in its unused state if we are
235 * closing an object that wasn't fully set up.
237 if (__builtin_expect(old_map != NULL, 1)) {
238 _dl_assert(old_map->l_tls_modid == idx);
240 /* Mark the entry as unused. */
241 listp->slotinfo[idx - disp].gen = _dl_tls_generation + 1;
242 listp->slotinfo[idx - disp].map = NULL;
246 * If this is not the last currently used entry no need to
247 * look further.
249 if (idx != _dl_tls_max_dtv_idx)
250 return true;
253 while (idx - disp > (disp == 0 ? 1 + _dl_tls_static_nelem : 0)) {
254 --idx;
256 if (listp->slotinfo[idx - disp].map != NULL) {
257 /* Found a new last used index. */
258 _dl_tls_max_dtv_idx = idx;
259 return true;
263 /* No non-entry in this list element. */
264 return false;
266 #endif
268 static ptrdiff_t _dl_build_local_scope (struct elf_resolve **list,
269 struct elf_resolve *map)
271 struct elf_resolve **p = list;
272 struct init_fini_list *q;
274 *p++ = map;
275 map->init_flag |= DL_RESERVED;
276 if (map->init_fini)
277 for (q = map->init_fini; q; q = q->next)
278 if (! (q->tpnt->init_flag & DL_RESERVED))
279 p += _dl_build_local_scope (p, q->tpnt);
280 return p - list;
283 static void *do_dlopen(const char *libname, int flag, ElfW(Addr) from)
285 struct elf_resolve *tpnt, *tfrom;
286 struct dyn_elf *dyn_chain, *rpnt = NULL, *dyn_ptr, *relro_ptr, *handle;
287 struct elf_resolve *tpnt1;
288 void (*dl_brk) (void);
289 int now_flag;
290 struct init_fini_list *tmp, *runp, *runp2, *dep_list;
291 unsigned int nlist, i;
292 struct elf_resolve **init_fini_list;
293 static bool _dl_init;
294 struct elf_resolve **local_scope;
295 #ifdef SHARED
296 struct r_scope_elem *ls;
297 #endif
298 #if defined(USE_TLS) && USE_TLS
299 bool any_tls = false;
300 #endif
302 /* A bit of sanity checking... */
303 if (!(flag & (RTLD_LAZY|RTLD_NOW|RTLD_NOLOAD))) {
304 _dl_error_number = LD_BAD_HANDLE;
305 return NULL;
308 if (!_dl_init) {
309 _dl_init = true;
310 _dl_malloc_function = malloc;
311 _dl_free_function = free;
313 /* Cover the trivial case first */
314 if (!libname)
315 return _dl_symbol_tables;
317 #ifndef SHARED
318 # ifdef __SUPPORT_LD_DEBUG__
319 _dl_debug = getenv("LD_DEBUG");
320 if (_dl_debug) {
321 if (strstr(_dl_debug, "all")) {
322 _dl_debug_detail = _dl_debug_move = _dl_debug_symbols
323 = _dl_debug_reloc = _dl_debug_bindings = _dl_debug_nofixups = (void*)1;
324 } else {
325 _dl_debug_detail = strstr(_dl_debug, "detail");
326 _dl_debug_move = strstr(_dl_debug, "move");
327 _dl_debug_symbols = strstr(_dl_debug, "sym");
328 _dl_debug_reloc = strstr(_dl_debug, "reloc");
329 _dl_debug_nofixups = strstr(_dl_debug, "nofix");
330 _dl_debug_bindings = strstr(_dl_debug, "bind");
333 # endif
334 #endif
336 _dl_map_cache();
339 * Try and locate the module we were called from - we
340 * need this so that we get the correct RPATH/RUNPATH. Note that
341 * this is the current behavior under Solaris, but the
342 * ABI+ specifies that we should only use the RPATH from
343 * the application. Thus this may go away at some time
344 * in the future.
347 struct dyn_elf *dpnt;
348 tfrom = NULL;
349 for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
350 tpnt = dpnt->dyn;
351 if (DL_ADDR_IN_LOADADDR(from, tpnt, tfrom))
352 tfrom = tpnt;
355 for (rpnt = _dl_symbol_tables; rpnt && rpnt->next; rpnt = rpnt->next)
356 continue;
358 relro_ptr = rpnt;
359 now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
360 if (getenv("LD_BIND_NOW"))
361 now_flag = RTLD_NOW;
363 #if !defined SHARED && defined __LDSO_LD_LIBRARY_PATH__
364 /* When statically linked, the _dl_library_path is not yet initialized */
365 _dl_library_path = getenv("LD_LIBRARY_PATH");
366 #endif
368 /* Try to load the specified library */
369 _dl_if_debug_print("Trying to dlopen '%s', RTLD_GLOBAL:%d RTLD_NOW:%d\n",
370 (char*)libname, (flag & RTLD_GLOBAL ? 1:0), (now_flag & RTLD_NOW ? 1:0));
372 tpnt = _dl_load_shared_library(flag & (RTLD_NOLOAD | RTLD_GLOBAL | RTLD_NODELETE),
373 &rpnt, tfrom, (char*)libname, 0);
374 if (tpnt == NULL) {
375 _dl_unmap_cache();
376 return NULL;
378 dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
379 memset(dyn_chain, 0, sizeof(struct dyn_elf));
380 dyn_chain->dyn = tpnt;
382 dyn_chain->next_handle = _dl_handles;
383 _dl_handles = dyn_ptr = dyn_chain;
385 if (tpnt->init_flag & DL_OPENED2) {
386 _dl_if_debug_print("Lib: %s already opened\n", libname);
387 /* see if there is a handle from a earlier dlopen */
388 for (handle = _dl_handles->next_handle; handle; handle = handle->next_handle) {
389 if (handle->dyn == tpnt) {
390 dyn_chain->init_fini.init_fini = handle->init_fini.init_fini;
391 dyn_chain->init_fini.nlist = handle->init_fini.nlist;
392 for (i = 0; i < dyn_chain->init_fini.nlist; i++)
393 dyn_chain->init_fini.init_fini[i]->rtld_flags |= (flag & (RTLD_GLOBAL|RTLD_NODELETE));
394 dyn_chain->next = handle->next;
395 break;
398 return dyn_chain;
401 _dl_if_debug_print("Looking for needed libraries\n");
402 nlist = 0;
403 runp = alloca(sizeof(*runp));
404 runp->tpnt = tpnt;
405 runp->next = NULL;
406 dep_list = runp2 = runp;
407 for (; runp; runp = runp->next) {
408 ElfW(Dyn) *dpnt;
409 char *lpntstr;
411 nlist++;
412 runp->tpnt->init_fini = NULL; /* clear any previous dependcies */
413 for (dpnt = (ElfW(Dyn) *) runp->tpnt->dynamic_addr; dpnt->d_tag; dpnt++) {
414 if (dpnt->d_tag == DT_NEEDED) {
415 lpntstr = (char*) (runp->tpnt->dynamic_info[DT_STRTAB] +
416 dpnt->d_un.d_val);
417 _dl_if_debug_print("Trying to load '%s', needed by '%s'\n",
418 lpntstr, runp->tpnt->libname);
419 tpnt1 = _dl_load_shared_library(flag & (RTLD_GLOBAL | RTLD_NODELETE),
420 &rpnt, runp->tpnt, lpntstr, 0);
421 if (!tpnt1)
422 goto oops;
424 /* This list is for dlsym() and relocation */
425 dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
426 memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
427 dyn_ptr = dyn_ptr->next;
428 dyn_ptr->dyn = tpnt1;
429 /* Used to record RTLD_LOCAL scope */
430 tmp = alloca(sizeof(struct init_fini_list));
431 tmp->tpnt = tpnt1;
432 tmp->next = runp->tpnt->init_fini;
433 runp->tpnt->init_fini = tmp;
435 for (tmp=dep_list; tmp; tmp = tmp->next) {
436 if (tpnt1 == tmp->tpnt) { /* if match => cirular dependency, drop it */
437 _dl_if_debug_print("Circular dependency, skipping '%s',\n",
438 tmp->tpnt->libname);
439 tpnt1->usage_count--;
440 break;
443 if (!tmp) { /* Don't add if circular dependency detected */
444 runp2->next = alloca(sizeof(*runp));
445 runp2 = runp2->next;
446 runp2->tpnt = tpnt1;
447 runp2->next = NULL;
452 init_fini_list = malloc(nlist * sizeof(struct elf_resolve *));
453 dyn_chain->init_fini.init_fini = init_fini_list;
454 dyn_chain->init_fini.nlist = nlist;
455 i = 0;
456 for (runp2 = dep_list; runp2; runp2 = runp2->next) {
457 init_fini_list[i++] = runp2->tpnt;
458 for (runp = runp2->tpnt->init_fini; runp; runp = runp->next) {
459 if (!(runp->tpnt->rtld_flags & RTLD_GLOBAL)) {
460 tmp = malloc(sizeof(struct init_fini_list));
461 tmp->tpnt = runp->tpnt;
462 tmp->next = runp2->tpnt->rtld_local;
463 runp2->tpnt->rtld_local = tmp;
468 /* Build the local scope for the dynamically loaded modules. */
469 local_scope = _dl_malloc(nlist * sizeof(struct elf_resolve *)); /* Could it allocated on stack? */
470 for (i = 0; i < nlist; i++)
471 if (init_fini_list[i]->symbol_scope.r_nlist == 0) {
472 int k, cnt;
473 cnt = _dl_build_local_scope(local_scope, init_fini_list[i]);
474 init_fini_list[i]->symbol_scope.r_list = _dl_malloc(cnt * sizeof(struct elf_resolve *));
475 init_fini_list[i]->symbol_scope.r_nlist = cnt;
476 _dl_memcpy (init_fini_list[i]->symbol_scope.r_list, local_scope,
477 cnt * sizeof (struct elf_resolve *));
478 /* Restoring the init_flag.*/
479 for (k = 0; k < nlist; k++)
480 init_fini_list[k]->init_flag &= ~DL_RESERVED;
483 _dl_free(local_scope);
485 /* Sort the INIT/FINI list in dependency order. */
486 for (runp2 = dep_list; runp2; runp2 = runp2->next) {
487 unsigned int j, k;
488 for (j = 0; init_fini_list[j] != runp2->tpnt; ++j)
489 /* Empty */;
490 for (k = j + 1; k < nlist; ++k) {
491 struct init_fini_list *ele = init_fini_list[k]->init_fini;
493 for (; ele; ele = ele->next) {
494 if (ele->tpnt == runp2->tpnt) {
495 struct elf_resolve *here = init_fini_list[k];
496 _dl_if_debug_print("Move %s from pos %d to %d in INIT/FINI list.\n", here->libname, k, j);
497 for (i = (k - j); i; --i)
498 init_fini_list[i+j] = init_fini_list[i+j-1];
499 init_fini_list[j] = here;
500 ++j;
501 break;
506 #ifdef __SUPPORT_LD_DEBUG__
507 if (_dl_debug) {
508 fprintf(stderr, "\nINIT/FINI order and dependencies:\n");
509 for (i = 0; i < nlist; i++) {
510 fprintf(stderr, "lib: %s has deps:\n", init_fini_list[i]->libname);
511 runp = init_fini_list[i]->init_fini;
512 for (; runp; runp = runp->next)
513 fprintf(stderr, " %s ", runp->tpnt->libname);
514 fprintf(stderr, "\n");
517 #endif
519 _dl_if_debug_print("Beginning dlopen relocation fixups\n");
521 * OK, now all of the kids are tucked into bed in their proper addresses.
522 * Now we go through and look for REL and RELA records that indicate fixups
523 * to the GOT tables. We need to do this in reverse order so that COPY
524 * directives work correctly */
526 #ifdef SHARED
528 * Get the tail of the list.
529 * In the static case doesn't need to extend the global scope, it is
530 * ready to be used as it is, because _dl_loaded_modules already points
531 * to the dlopened library.
533 for (ls = &_dl_loaded_modules->symbol_scope; ls && ls->next; ls = ls->next);
535 /* Extend the global scope by adding the local scope of the dlopened DSO. */
536 ls->next = &dyn_chain->dyn->symbol_scope;
537 #endif
538 #ifdef __mips__
540 * Relocation of the GOT entries for MIPS have to be done
541 * after all the libraries have been loaded.
543 _dl_perform_mips_global_got_relocations(tpnt, !now_flag);
544 #endif
546 if (_dl_fixup(dyn_chain, &_dl_loaded_modules->symbol_scope, now_flag))
547 goto oops;
549 if (relro_ptr) {
550 for (rpnt = relro_ptr->next; rpnt; rpnt = rpnt->next) {
551 if (rpnt->dyn->relro_size)
552 _dl_protect_relro(rpnt->dyn);
555 /* TODO: Should we set the protections of all pages back to R/O now ? */
558 #if defined(USE_TLS) && USE_TLS
560 for (i=0; i < nlist; i++) {
561 struct elf_resolve *tmp_tpnt = init_fini_list[i];
562 /* Only add TLS memory if this object is loaded now and
563 therefore is not yet initialized. */
565 if (!(tmp_tpnt->init_flag & INIT_FUNCS_CALLED)
566 /* Only if the module defines thread local data. */
567 && __builtin_expect (tmp_tpnt->l_tls_blocksize > 0, 0)) {
569 /* Now that we know the object is loaded successfully add
570 modules containing TLS data to the slot info table. We
571 might have to increase its size. */
572 _dl_add_to_slotinfo ((struct link_map*)tmp_tpnt);
574 /* It is the case in which we couldn't perform TLS static
575 initialization at relocation time, and we delayed it until
576 the relocation has been completed. */
578 if (tmp_tpnt->l_need_tls_init) {
579 tmp_tpnt->l_need_tls_init = 0;
580 # ifdef SHARED
581 /* Update the slot information data for at least the
582 generation of the DSO we are allocating data for. */
583 _dl_update_slotinfo (tmp_tpnt->l_tls_modid);
584 # endif
586 _dl_init_static_tls((struct link_map*)tmp_tpnt);
587 _dl_assert (tmp_tpnt->l_need_tls_init == 0);
590 /* We have to bump the generation counter. */
591 any_tls = true;
595 /* Bump the generation number if necessary. */
596 if (any_tls && __builtin_expect (++_dl_tls_generation == 0, 0)) {
597 _dl_debug_early("TLS generation counter wrapped! Please report this.");
598 _dl_exit(30);
601 #endif
603 /* Notify the debugger we have added some objects. */
604 if (_dl_debug_addr) {
605 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
606 if (dl_brk != NULL) {
607 _dl_debug_addr->r_state = RT_ADD;
608 (*dl_brk) ();
610 _dl_debug_addr->r_state = RT_CONSISTENT;
611 (*dl_brk) ();
615 /* Run the ctors and setup the dtors */
616 for (i = nlist; i; --i) {
617 tpnt = init_fini_list[i-1];
618 if (tpnt->init_flag & INIT_FUNCS_CALLED)
619 continue;
620 tpnt->init_flag |= INIT_FUNCS_CALLED;
622 if (tpnt->dynamic_info[DT_INIT] != NULL) {
623 void (*dl_elf_func) (void);
624 dl_elf_func = (void (*)(void)) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_INIT]);
625 if (dl_elf_func) {
626 _dl_if_debug_print("running old-style ctors for library %s at '%p'\n",
627 tpnt->libname, dl_elf_func);
628 DL_CALL_FUNC_AT_ADDR (dl_elf_func, tpnt->loadaddr, (void(*)(void)));
632 if (tpnt->dynamic_info[DT_INIT_ARRAY] != NULL) {
633 void (*dl_elf_func) (void);
634 dl_elf_func = (void (*)(void)) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_INIT_ARRAY]);
635 if (dl_elf_func) {
636 _dl_if_debug_print("running ctors for library %s at '%p'\n",
637 tpnt->libname, dl_elf_func);
638 _dl_run_init_array(tpnt);
643 _dl_unmap_cache();
644 return (void *) dyn_chain;
646 oops:
647 /* Something went wrong. Clean up and return NULL. */
648 _dl_unmap_cache();
649 do_dlclose(dyn_chain, 0);
650 return NULL;
653 void *dlopen(const char *libname, int flag)
655 void *ret;
657 __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex, 1);
658 ret = do_dlopen(libname, flag,
659 (ElfW(Addr)) __builtin_return_address(0));
660 __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex, 1);
662 return ret;
665 static void *do_dlsym(void *vhandle, const char *name, void *caller_address)
667 struct elf_resolve *tpnt, *tfrom;
668 struct dyn_elf *handle;
669 ElfW(Addr) from = 0;
670 struct dyn_elf *rpnt;
671 void *ret;
672 struct symbol_ref sym_ref = { NULL, NULL };
673 /* Nastiness to support underscore prefixes. */
674 #ifdef __UCLIBC_UNDERSCORES__
675 char tmp_buf[80];
676 char *name2 = tmp_buf;
677 size_t nlen = strlen (name) + 1;
678 if (nlen + 1 > sizeof (tmp_buf))
679 name2 = malloc (nlen + 1);
680 if (name2 == 0) {
681 _dl_error_number = LD_ERROR_MMAP_FAILED;
682 return 0;
684 name2[0] = '_';
685 memcpy (name2 + 1, name, nlen);
686 #else
687 const char *name2 = name;
688 #endif
689 handle = (struct dyn_elf *) vhandle;
691 /* First of all verify that we have a real handle
692 of some kind. Return NULL if not a valid handle. */
694 if (handle == NULL)
695 handle = _dl_symbol_tables;
696 else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
697 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
698 if (rpnt == handle)
699 break;
700 if (!rpnt) {
701 _dl_error_number = LD_BAD_HANDLE;
702 ret = NULL;
703 goto out;
705 } else if (handle == RTLD_NEXT) {
707 * Try and locate the module we were called from - we
708 * need this so that we know where to start searching
709 * from. We never pass RTLD_NEXT down into the actual
710 * dynamic loader itself, as it doesn't know
711 * how to properly treat it.
713 from = (ElfW(Addr)) caller_address;
715 tfrom = NULL;
716 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
717 tpnt = rpnt->dyn;
718 if (DL_ADDR_IN_LOADADDR(from, tpnt, tfrom)) {
719 tfrom = tpnt;
720 handle = rpnt->next;
724 tpnt = NULL;
725 if (handle == _dl_symbol_tables)
726 tpnt = handle->dyn; /* Only search RTLD_GLOBAL objs if global object */
728 do {
729 ret = _dl_find_hash(name2, &handle->dyn->symbol_scope, tpnt, ELF_RTYPE_CLASS_DLSYM, &sym_ref);
730 if (ret != NULL)
731 break;
732 handle = handle->next;
733 } while (from && handle);
735 #if defined(USE_TLS) && USE_TLS && defined SHARED
736 if (sym_ref.sym && (ELF_ST_TYPE(sym_ref.sym->st_info) == STT_TLS) && (sym_ref.tpnt)) {
737 /* The found symbol is a thread-local storage variable.
738 Return its address for the current thread. */
739 ret = _dl_tls_symaddr ((struct link_map *)sym_ref.tpnt, (Elf32_Addr)ret);
741 #endif
744 * Nothing found.
746 if (!ret)
747 _dl_error_number = LD_NO_SYMBOL;
748 out:
749 #ifdef __UCLIBC_UNDERSCORES__
750 if (name2 != tmp_buf)
751 free (name2);
752 #endif
753 return ret;
756 void *dlsym(void *vhandle, const char *name)
758 void *ret;
760 __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex, 1);
761 ret = do_dlsym(vhandle, name, __builtin_return_address(0));
762 __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex, 1);
764 return ret;
767 static int do_dlclose(void *vhandle, int need_fini)
769 struct dyn_elf *rpnt, *rpnt1, *rpnt1_tmp;
770 struct init_fini_list *runp, *tmp;
771 ElfW(Phdr) *ppnt;
772 struct elf_resolve *tpnt, *run_tpnt;
773 int (*dl_elf_fini) (void);
774 void (*dl_brk) (void);
775 struct dyn_elf *handle;
776 ElfW(Addr) end = 0, start = (ElfW(Addr))(~0ULL);
777 unsigned int i, j;
778 struct r_scope_elem *ls, *ls_next = NULL;
779 struct elf_resolve **handle_rlist;
781 #if defined(USE_TLS) && USE_TLS
782 bool any_tls = false;
783 size_t tls_free_start = NO_TLS_OFFSET;
784 size_t tls_free_end = NO_TLS_OFFSET;
785 struct link_map *tls_lmap;
786 #endif
788 handle = (struct dyn_elf *) vhandle;
789 if (handle == _dl_symbol_tables)
790 return 0;
791 rpnt1 = NULL;
792 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
793 if (rpnt == handle)
794 break;
795 rpnt1 = rpnt;
798 if (!rpnt) {
799 _dl_error_number = LD_BAD_HANDLE;
800 return 1;
802 if (rpnt1)
803 rpnt1->next_handle = rpnt->next_handle;
804 else
805 _dl_handles = rpnt->next_handle;
806 _dl_if_debug_print("%s: usage count: %d\n",
807 handle->dyn->libname, handle->dyn->usage_count);
808 if (handle->dyn->usage_count != 1) {
809 handle->dyn->usage_count--;
810 free(handle);
811 return 0;
814 /* Store the handle's local scope array for later removal */
815 handle_rlist = handle->dyn->symbol_scope.r_list;
817 /* Store references to the local scope entries for later removal */
818 for (ls = &_dl_loaded_modules->symbol_scope; ls && ls->next; ls = ls->next)
819 if (ls->next->r_list[0] == handle->dyn) {
820 break;
822 /* ls points to the previous local symbol scope */
823 if(ls && ls->next)
824 ls_next = ls->next->next;
826 /* OK, this is a valid handle - now close out the file */
827 for (j = 0; j < handle->init_fini.nlist; ++j) {
828 tpnt = handle->init_fini.init_fini[j];
829 tpnt->usage_count--;
830 if (tpnt->usage_count == 0) {
831 if ((tpnt->dynamic_info[DT_FINI]
832 || tpnt->dynamic_info[DT_FINI_ARRAY])
833 && need_fini
834 && !(tpnt->init_flag & FINI_FUNCS_CALLED)
836 tpnt->init_flag |= FINI_FUNCS_CALLED;
838 if (tpnt->dynamic_info[DT_FINI_ARRAY] != NULL) {
839 dl_elf_fini = (int (*)(void)) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_FINI_ARRAY]);
840 _dl_if_debug_print("running dtors for library %s at '%p'\n",
841 tpnt->libname, dl_elf_fini);
842 _dl_run_fini_array(tpnt);
845 if (tpnt->dynamic_info[DT_FINI] != NULL) {
846 dl_elf_fini = (int (*)(void)) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_FINI]);
847 _dl_if_debug_print("running old-style dtors for library %s at '%p'\n",
848 tpnt->libname, dl_elf_fini);
849 DL_CALL_FUNC_AT_ADDR (dl_elf_fini, tpnt->loadaddr, (int (*)(void)));
853 _dl_if_debug_print("unmapping: %s\n", tpnt->libname);
854 end = 0;
855 for (i = 0, ppnt = tpnt->ppnt;
856 i < tpnt->n_phent; ppnt++, i++) {
857 if (ppnt->p_type != PT_LOAD)
858 continue;
859 if (end < ppnt->p_vaddr + ppnt->p_memsz)
860 end = ppnt->p_vaddr + ppnt->p_memsz;
863 #if defined(USE_TLS) && USE_TLS
864 /* Do the cast to make things easy. */
865 tls_lmap = (struct link_map *) tpnt;
867 /* Remove the object from the dtv slotinfo array if it uses TLS. */
868 if (__builtin_expect (tls_lmap->l_tls_blocksize > 0, 0)) {
869 any_tls = true;
871 if (_dl_tls_dtv_slotinfo_list != NULL
872 && ! remove_slotinfo (tls_lmap->l_tls_modid,
873 _dl_tls_dtv_slotinfo_list, 0,
874 (tpnt->init_flag & INIT_FUNCS_CALLED)))
875 /* All dynamically loaded modules with TLS are unloaded. */
876 _dl_tls_max_dtv_idx = _dl_tls_static_nelem;
878 if (tls_lmap->l_tls_offset != NO_TLS_OFFSET) {
880 * Collect a contiguous chunk built from the objects in
881 * this search list, going in either direction. When the
882 * whole chunk is at the end of the used area then we can
883 * reclaim it.
885 # if defined(TLS_TCB_AT_TP)
886 if (tls_free_start == NO_TLS_OFFSET
887 || (size_t) tls_lmap->l_tls_offset == tls_free_start) {
888 /* Extend the contiguous chunk being reclaimed. */
889 tls_free_start
890 = tls_lmap->l_tls_offset -
891 tls_lmap->l_tls_blocksize;
893 if (tls_free_end == NO_TLS_OFFSET)
894 tls_free_end = tls_lmap->l_tls_offset;
895 } else if (tls_lmap->l_tls_offset - tls_lmap->l_tls_blocksize
896 == tls_free_end)
897 /* Extend the chunk backwards. */
898 tls_free_end = tls_lmap->l_tls_offset;
899 else {
901 * This isn't contiguous with the last chunk freed.
902 * One of them will be leaked unless we can free
903 * one block right away.
905 if (tls_free_end == _dl_tls_static_used) {
906 _dl_tls_static_used = tls_free_start;
907 tls_free_end = tls_lmap->l_tls_offset;
908 tls_free_start
909 = tls_free_end - tls_lmap->l_tls_blocksize;
910 } else if ((size_t) tls_lmap->l_tls_offset
911 == _dl_tls_static_used)
912 _dl_tls_static_used = tls_lmap->l_tls_offset -
913 tls_lmap->l_tls_blocksize;
914 else if (tls_free_end < (size_t) tls_lmap->l_tls_offset) {
916 * We pick the later block. It has a chance
917 * to be freed.
919 tls_free_end = tls_lmap->l_tls_offset;
920 tls_free_start = tls_free_end -
921 tls_lmap->l_tls_blocksize;
924 # elif defined(TLS_DTV_AT_TP)
925 if ((size_t) tls_lmap->l_tls_offset == tls_free_end)
926 /* Extend the contiguous chunk being reclaimed. */
927 tls_free_end -= tls_lmap->l_tls_blocksize;
928 else if (tls_lmap->l_tls_offset + tls_lmap->l_tls_blocksize
929 == tls_free_start)
930 /* Extend the chunk backwards. */
931 tls_free_start = tls_lmap->l_tls_offset;
932 else {
934 * This isn't contiguous with the last chunk
935 * freed. One of them will be leaked.
937 if (tls_free_end == _dl_tls_static_used)
938 _dl_tls_static_used = tls_free_start;
939 tls_free_start = tls_lmap->l_tls_offset;
940 tls_free_end = tls_free_start +
941 tls_lmap->l_tls_blocksize;
943 # else
944 # error Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined
945 # endif
946 } else {
948 #define TLS_DTV_UNALLOCATED ((void *) -1l)
950 dtv_t *dtv = THREAD_DTV ();
952 if (!(dtv[tls_lmap->l_tls_modid].pointer.is_static) &&
953 dtv[tls_lmap->l_tls_modid].pointer.val != TLS_DTV_UNALLOCATED) {
954 /* Note that free is called for NULL is well. We
955 deallocate even if it is this dtv entry we are
956 supposed to load. The reason is that we call
957 memalign and not malloc. */
958 _dl_free (dtv[tls_lmap->l_tls_modid].pointer.val);
959 dtv[tls_lmap->l_tls_modid].pointer.val = TLS_DTV_UNALLOCATED;
963 #endif
965 start = tpnt->mapaddr;
966 _dl_if_debug_print("unmapping before alignment: %s start: '%p' end: '%p'\n", tpnt->libname, start, end);
967 end = (end + ADDR_ALIGN) & PAGE_ALIGN;
968 start = start & ~ADDR_ALIGN;
969 _dl_if_debug_print("unmapping: %s start: '%p' end: '%p'\n", tpnt->libname, start, end);
970 DL_LIB_UNMAP (tpnt, end - start);
971 /* Free elements in RTLD_LOCAL scope list */
972 for (runp = tpnt->rtld_local; runp; runp = tmp) {
973 tmp = runp->next;
974 free(runp);
977 /* Next, remove tpnt from the loaded_module list */
978 if (_dl_loaded_modules == tpnt) {
979 _dl_loaded_modules = tpnt->next;
980 if (_dl_loaded_modules)
981 _dl_loaded_modules->prev = 0;
982 } else {
983 for (run_tpnt = _dl_loaded_modules; run_tpnt; run_tpnt = run_tpnt->next) {
984 if (run_tpnt->next == tpnt) {
985 _dl_if_debug_print("removing loaded_modules: %s\n", tpnt->libname);
986 run_tpnt->next = run_tpnt->next->next;
987 if (run_tpnt->next)
988 run_tpnt->next->prev = run_tpnt;
989 break;
994 /* Next, remove tpnt from the global symbol table list */
995 if (_dl_symbol_tables) {
996 if (_dl_symbol_tables->dyn == tpnt) {
997 _dl_symbol_tables = _dl_symbol_tables->next;
998 if (_dl_symbol_tables)
999 _dl_symbol_tables->prev = 0;
1000 } else {
1001 for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
1002 if (rpnt1->next->dyn == tpnt) {
1003 _dl_if_debug_print("removing symbol_tables: %s\n", tpnt->libname);
1004 rpnt1_tmp = rpnt1->next->next;
1005 free(rpnt1->next);
1006 rpnt1->next = rpnt1_tmp;
1007 if (rpnt1->next)
1008 rpnt1->next->prev = rpnt1;
1009 break;
1014 free(tpnt->libname);
1015 if (handle->dyn != tpnt)
1016 free(tpnt->symbol_scope.r_list);
1017 free(tpnt);
1020 /* Unlink and release the handle's local scope from global one */
1021 if(ls)
1022 ls->next = ls_next;
1023 free(handle_rlist);
1025 for (rpnt1 = handle->next; rpnt1; rpnt1 = rpnt1_tmp) {
1026 rpnt1_tmp = rpnt1->next;
1027 free(rpnt1);
1029 free(handle->init_fini.init_fini);
1030 free(handle);
1032 #if defined(USE_TLS) && USE_TLS
1033 /* If we removed any object which uses TLS bump the generation counter. */
1034 if (any_tls) {
1035 if (__builtin_expect(++_dl_tls_generation == 0, 0)) {
1036 _dl_debug_early("TLS generation counter wrapped! Please report to the uClibc mailing list.\n");
1037 _dl_exit(30);
1040 if (tls_free_end == _dl_tls_static_used)
1041 _dl_tls_static_used = tls_free_start;
1043 #endif
1045 if (_dl_debug_addr) {
1046 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
1047 if (dl_brk != NULL) {
1048 _dl_debug_addr->r_state = RT_DELETE;
1049 (*dl_brk) ();
1051 _dl_debug_addr->r_state = RT_CONSISTENT;
1052 (*dl_brk) ();
1056 return 0;
1059 int dlclose(void *vhandle)
1061 int ret;
1063 __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex, 1);
1064 ret = do_dlclose(vhandle, 1);
1065 __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex, 1);
1067 return ret;
1070 char *dlerror(void)
1072 const char *retval;
1074 if (!_dl_error_number)
1075 return NULL;
1076 retval = dl_error_names[_dl_error_number];
1077 _dl_error_number = 0;
1078 return (char *)retval;
1081 #ifdef __USE_GNU
1082 static int do_dladdr(const void *__address, Dl_info * __info)
1084 struct elf_resolve *pelf;
1085 struct elf_resolve *rpnt;
1087 _dl_map_cache();
1090 * Try and locate the module address is in
1092 pelf = NULL;
1094 _dl_if_debug_print("__address: %p __info: %p\n", __address, __info);
1096 __address = DL_LOOKUP_ADDRESS (__address);
1098 for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
1099 struct elf_resolve *tpnt;
1101 tpnt = rpnt;
1103 _dl_if_debug_print("Module \"%s\" at %p\n",
1104 tpnt->libname, DL_LOADADDR_BASE(tpnt->loadaddr));
1106 if (DL_ADDR_IN_LOADADDR((ElfW(Addr)) __address, tpnt, pelf))
1107 pelf = tpnt;
1110 if (!pelf) {
1111 return 0;
1115 * Try and locate the symbol of address
1119 char *strtab;
1120 ElfW(Sym) *symtab;
1121 unsigned int hn, si, sn, sf;
1122 ElfW(Addr) sa = 0;
1124 /* Set the info for the object the address lies in */
1125 __info->dli_fname = pelf->libname;
1126 __info->dli_fbase = (void *)pelf->mapaddr;
1128 symtab = (ElfW(Sym) *) (pelf->dynamic_info[DT_SYMTAB]);
1129 strtab = (char *) (pelf->dynamic_info[DT_STRTAB]);
1131 sf = sn = 0;
1133 #ifdef __LDSO_GNU_HASH_SUPPORT__
1134 if (pelf->l_gnu_bitmask) {
1135 for (hn = 0; hn < pelf->nbucket; hn++) {
1136 si = pelf->l_gnu_buckets[hn];
1137 if (!si)
1138 continue;
1140 const Elf32_Word *hasharr = &pelf->l_gnu_chain_zero[si];
1141 do {
1142 ElfW(Addr) symbol_addr;
1144 symbol_addr = (ElfW(Addr)) DL_RELOC_ADDR(pelf->loadaddr, symtab[si].st_value);
1145 if ((symtab[si].st_shndx != SHN_UNDEF
1146 || symtab[si].st_value != 0)
1147 && ELF_ST_TYPE(symtab[si].st_info) != STT_TLS
1148 && DL_ADDR_SYM_MATCH(symbol_addr, &symtab[si], sa,
1149 (ElfW(Addr)) __address)) {
1150 sa = symbol_addr;
1151 sn = si;
1152 sf = 1;
1154 _dl_if_debug_print("Symbol \"%s\" at %p\n", strtab + symtab[si].st_name, symbol_addr);
1155 ++si;
1156 } while ((*hasharr++ & 1u) == 0);
1158 } else
1159 #endif
1160 for (hn = 0; hn < pelf->nbucket; hn++) {
1161 for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
1162 ElfW(Addr) symbol_addr;
1164 symbol_addr = (ElfW(Addr)) DL_RELOC_ADDR(pelf->loadaddr, symtab[si].st_value);
1165 if ((symtab[si].st_shndx != SHN_UNDEF
1166 || symtab[si].st_value != 0)
1167 && ELF_ST_TYPE(symtab[si].st_info) != STT_TLS
1168 && DL_ADDR_SYM_MATCH(symbol_addr, &symtab[si], sa,
1169 (ElfW(Addr)) __address)) {
1170 sa = symbol_addr;
1171 sn = si;
1172 sf = 1;
1175 _dl_if_debug_print("Symbol \"%s\" at %p\n",
1176 strtab + symtab[si].st_name, symbol_addr);
1180 if (sf) {
1181 /* A nearest symbol has been found; fill the entries */
1182 __info->dli_sname = strtab + symtab[sn].st_name;
1183 __info->dli_saddr = (void *)sa;
1184 } else {
1185 /* No symbol found, fill entries with NULL value,
1186 only the containing object will be returned. */
1187 __info->dli_sname = NULL;
1188 __info->dli_saddr = NULL;
1190 return 1;
1193 #endif
1195 int dladdr(const void *__address, Dl_info * __info)
1197 int ret;
1199 __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex, 1);
1200 ret = do_dladdr(__address, __info);
1201 __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex, 1);
1203 return ret;