libdl: remove dead code
[uclibc-ng.git] / ldso / libdl / libdl.c
blobb72318accdfdd730d63f23c668b60a563b751955
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"
120 # if defined(USE_TLS) && USE_TLS
122 * Giving this initialized value preallocates some surplus bytes in the
123 * static TLS area, see __libc_setup_tls (libc-tls.c).
125 size_t _dl_tls_static_size = 2048;
126 # endif
127 #include LDSO_ELFINTERP
128 #include "../ldso/dl-hash.c"
129 #define _dl_trace_loaded_objects 0
130 #include "../ldso/dl-elf.c"
131 #endif /* SHARED */
133 #ifdef __SUPPORT_LD_DEBUG__
134 # define _dl_if_debug_print(fmt, args...) \
135 do { \
136 if (_dl_debug) \
137 fprintf(stderr, "%s():%i: " fmt, __func__, __LINE__, ## args); \
138 } while (0)
139 #else
140 # define _dl_if_debug_print(fmt, args...)
141 #endif
143 static int do_dlclose(void *, int need_fini);
146 static const char *const dl_error_names[] = {
148 "File not found",
149 "Unable to open /dev/zero",
150 "Not an ELF file",
151 #if defined (__i386__)
152 "Not i386 binary",
153 #elif defined (__sparc__)
154 "Not sparc binary",
155 #elif defined (__mc68000__)
156 "Not m68k binary",
157 #else
158 "Unrecognized binary type",
159 #endif
160 "Not an ELF shared library",
161 "Unable to mmap file",
162 "No dynamic section",
163 "Library contains unsupported TLS",
164 #ifdef ELF_USES_RELOCA
165 "Unable to process REL relocs",
166 #else
167 "Unable to process RELA relocs",
168 #endif
169 "Bad handle",
170 "Unable to resolve symbol"
174 #if defined(USE_TLS) && USE_TLS
175 #ifdef SHARED
177 * Systems which do not have tls_index also probably have to define
178 * DONT_USE_TLS_INDEX.
181 # ifndef __TLS_GET_ADDR
182 # define __TLS_GET_ADDR __tls_get_addr
183 # endif
186 * Return the symbol address given the map of the module it is in and
187 * the symbol record. This is used in dl-sym.c.
189 static void *
190 internal_function
191 _dl_tls_symaddr(struct link_map *map, const Elf32_Addr st_value)
193 # ifndef DONT_USE_TLS_INDEX
194 tls_index tmp =
196 .ti_module = map->l_tls_modid,
197 .ti_offset = st_value
200 return __TLS_GET_ADDR (&tmp);
201 # else
202 return __TLS_GET_ADDR (map->l_tls_modid, st_value);
203 # endif
205 #endif
207 /* Returns true when a non-empty entry was found. */
208 static bool
209 remove_slotinfo(size_t idx, struct dtv_slotinfo_list *listp, size_t disp,
210 bool should_be_there)
212 if (idx - disp >= listp->len) {
213 if (listp->next == NULL) {
215 * The index is not actually valid in the slotinfo list,
216 * because this object was closed before it was fully set
217 * up due to some error.
219 _dl_assert(!should_be_there);
220 } else {
221 if (remove_slotinfo(idx, listp->next, disp + listp->len,
222 should_be_there))
223 return true;
226 * No non-empty entry. Search from the end of this element's
227 * slotinfo array.
229 idx = disp + listp->len;
231 } else {
232 struct link_map *old_map = listp->slotinfo[idx - disp].map;
235 * The entry might still be in its unused state if we are
236 * closing an object that wasn't fully set up.
238 if (__builtin_expect(old_map != NULL, 1)) {
239 _dl_assert(old_map->l_tls_modid == idx);
241 /* Mark the entry as unused. */
242 listp->slotinfo[idx - disp].gen = _dl_tls_generation + 1;
243 listp->slotinfo[idx - disp].map = NULL;
247 * If this is not the last currently used entry no need to
248 * look further.
250 if (idx != _dl_tls_max_dtv_idx)
251 return true;
254 while (idx - disp > (disp == 0 ? 1 + _dl_tls_static_nelem : 0)) {
255 --idx;
257 if (listp->slotinfo[idx - disp].map != NULL) {
258 /* Found a new last used index. */
259 _dl_tls_max_dtv_idx = idx;
260 return true;
264 /* No non-entry in this list element. */
265 return false;
267 #endif
269 static ptrdiff_t _dl_build_local_scope (struct elf_resolve **list,
270 struct elf_resolve *map)
272 struct elf_resolve **p = list;
273 struct init_fini_list *q;
275 *p++ = map;
276 map->init_flag |= DL_RESERVED;
277 if (map->init_fini)
278 for (q = map->init_fini; q; q = q->next)
279 if (! (q->tpnt->init_flag & DL_RESERVED))
280 p += _dl_build_local_scope (p, q->tpnt);
281 return p - list;
284 static void *do_dlopen(const char *libname, int flag, ElfW(Addr) from)
286 struct elf_resolve *tpnt, *tfrom;
287 struct dyn_elf *dyn_chain, *rpnt = NULL, *dyn_ptr, *relro_ptr, *handle;
288 struct elf_resolve *tpnt1;
289 void (*dl_brk) (void);
290 int now_flag;
291 struct init_fini_list *tmp, *runp, *runp2, *dep_list;
292 unsigned int nlist, i;
293 struct elf_resolve **init_fini_list;
294 static bool _dl_init;
295 struct elf_resolve **local_scope;
296 #ifdef SHARED
297 struct r_scope_elem *ls;
298 #endif
299 #if defined(USE_TLS) && USE_TLS
300 bool any_tls = false;
301 #endif
303 /* A bit of sanity checking... */
304 if (!(flag & (RTLD_LAZY|RTLD_NOW|RTLD_NOLOAD))) {
305 _dl_error_number = LD_BAD_HANDLE;
306 return NULL;
309 if (!_dl_init) {
310 _dl_init = true;
311 _dl_malloc_function = malloc;
312 _dl_free_function = free;
314 /* Cover the trivial case first */
315 if (!libname)
316 return _dl_symbol_tables;
318 #ifndef SHARED
319 # ifdef __SUPPORT_LD_DEBUG__
320 _dl_debug = getenv("LD_DEBUG");
321 if (_dl_debug) {
322 if (strstr(_dl_debug, "all")) {
323 _dl_debug_detail = _dl_debug_move = _dl_debug_symbols
324 = _dl_debug_reloc = _dl_debug_bindings = _dl_debug_nofixups = (void*)1;
325 } else {
326 _dl_debug_detail = strstr(_dl_debug, "detail");
327 _dl_debug_move = strstr(_dl_debug, "move");
328 _dl_debug_symbols = strstr(_dl_debug, "sym");
329 _dl_debug_reloc = strstr(_dl_debug, "reloc");
330 _dl_debug_nofixups = strstr(_dl_debug, "nofix");
331 _dl_debug_bindings = strstr(_dl_debug, "bind");
334 # endif
335 #endif
337 _dl_map_cache();
340 * Try and locate the module we were called from - we
341 * need this so that we get the correct RPATH/RUNPATH. Note that
342 * this is the current behavior under Solaris, but the
343 * ABI+ specifies that we should only use the RPATH from
344 * the application. Thus this may go away at some time
345 * in the future.
348 struct dyn_elf *dpnt;
349 tfrom = NULL;
350 for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
351 tpnt = dpnt->dyn;
352 if (DL_ADDR_IN_LOADADDR(from, tpnt, tfrom))
353 tfrom = tpnt;
356 for (rpnt = _dl_symbol_tables; rpnt && rpnt->next; rpnt = rpnt->next)
357 continue;
359 relro_ptr = rpnt;
360 now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
361 if (getenv("LD_BIND_NOW"))
362 now_flag = RTLD_NOW;
364 #if !defined SHARED && defined __LDSO_LD_LIBRARY_PATH__
365 /* When statically linked, the _dl_library_path is not yet initialized */
366 _dl_library_path = getenv("LD_LIBRARY_PATH");
367 #endif
369 /* Try to load the specified library */
370 _dl_if_debug_print("Trying to dlopen '%s', RTLD_GLOBAL:%d RTLD_NOW:%d\n",
371 (char*)libname, (flag & RTLD_GLOBAL ? 1:0), (now_flag & RTLD_NOW ? 1:0));
373 tpnt = _dl_load_shared_library(flag & (RTLD_NOLOAD | RTLD_GLOBAL | RTLD_NODELETE),
374 &rpnt, tfrom, (char*)libname, 0);
375 if (tpnt == NULL) {
376 _dl_unmap_cache();
377 return NULL;
379 dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
380 memset(dyn_chain, 0, sizeof(struct dyn_elf));
381 dyn_chain->dyn = tpnt;
383 dyn_chain->next_handle = _dl_handles;
384 _dl_handles = dyn_ptr = dyn_chain;
386 if (tpnt->init_flag & DL_OPENED2) {
387 _dl_if_debug_print("Lib: %s already opened\n", libname);
388 /* see if there is a handle from a earlier dlopen */
389 for (handle = _dl_handles->next_handle; handle; handle = handle->next_handle) {
390 if (handle->dyn == tpnt) {
391 dyn_chain->init_fini.init_fini = handle->init_fini.init_fini;
392 dyn_chain->init_fini.nlist = handle->init_fini.nlist;
393 for (i = 0; i < dyn_chain->init_fini.nlist; i++)
394 dyn_chain->init_fini.init_fini[i]->rtld_flags |= (flag & (RTLD_GLOBAL|RTLD_NODELETE));
395 dyn_chain->next = handle->next;
396 break;
399 return dyn_chain;
402 _dl_if_debug_print("Looking for needed libraries\n");
403 nlist = 0;
404 runp = alloca(sizeof(*runp));
405 runp->tpnt = tpnt;
406 runp->next = NULL;
407 dep_list = runp2 = runp;
408 for (; runp; runp = runp->next) {
409 ElfW(Dyn) *dpnt;
410 char *lpntstr;
412 nlist++;
413 runp->tpnt->init_fini = NULL; /* clear any previous dependcies */
414 for (dpnt = (ElfW(Dyn) *) runp->tpnt->dynamic_addr; dpnt->d_tag; dpnt++) {
415 if (dpnt->d_tag == DT_NEEDED) {
416 lpntstr = (char*) (runp->tpnt->dynamic_info[DT_STRTAB] +
417 dpnt->d_un.d_val);
418 _dl_if_debug_print("Trying to load '%s', needed by '%s'\n",
419 lpntstr, runp->tpnt->libname);
420 tpnt1 = _dl_load_shared_library(flag & (RTLD_GLOBAL | RTLD_NODELETE),
421 &rpnt, runp->tpnt, lpntstr, 0);
422 if (!tpnt1)
423 goto oops;
425 /* This list is for dlsym() and relocation */
426 dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
427 memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
428 dyn_ptr = dyn_ptr->next;
429 dyn_ptr->dyn = tpnt1;
430 /* Used to record RTLD_LOCAL scope */
431 tmp = alloca(sizeof(struct init_fini_list));
432 tmp->tpnt = tpnt1;
433 tmp->next = runp->tpnt->init_fini;
434 runp->tpnt->init_fini = tmp;
436 for (tmp=dep_list; tmp; tmp = tmp->next) {
437 if (tpnt1 == tmp->tpnt) { /* if match => cirular dependency, drop it */
438 _dl_if_debug_print("Circular dependency, skipping '%s',\n",
439 tmp->tpnt->libname);
440 tpnt1->usage_count--;
441 break;
444 if (!tmp) { /* Don't add if circular dependency detected */
445 runp2->next = alloca(sizeof(*runp));
446 runp2 = runp2->next;
447 runp2->tpnt = tpnt1;
448 runp2->next = NULL;
453 init_fini_list = malloc(nlist * sizeof(struct elf_resolve *));
454 dyn_chain->init_fini.init_fini = init_fini_list;
455 dyn_chain->init_fini.nlist = nlist;
456 i = 0;
457 for (runp2 = dep_list; runp2; runp2 = runp2->next) {
458 init_fini_list[i++] = runp2->tpnt;
459 for (runp = runp2->tpnt->init_fini; runp; runp = runp->next) {
460 if (!(runp->tpnt->rtld_flags & RTLD_GLOBAL)) {
461 tmp = malloc(sizeof(struct init_fini_list));
462 tmp->tpnt = runp->tpnt;
463 tmp->next = runp2->tpnt->rtld_local;
464 runp2->tpnt->rtld_local = tmp;
469 /* Build the local scope for the dynamically loaded modules. */
470 local_scope = _dl_malloc(nlist * sizeof(struct elf_resolve *)); /* Could it allocated on stack? */
471 for (i = 0; i < nlist; i++)
472 if (init_fini_list[i]->symbol_scope.r_nlist == 0) {
473 int k, cnt;
474 cnt = _dl_build_local_scope(local_scope, init_fini_list[i]);
475 init_fini_list[i]->symbol_scope.r_list = _dl_malloc(cnt * sizeof(struct elf_resolve *));
476 init_fini_list[i]->symbol_scope.r_nlist = cnt;
477 _dl_memcpy (init_fini_list[i]->symbol_scope.r_list, local_scope,
478 cnt * sizeof (struct elf_resolve *));
479 /* Restoring the init_flag.*/
480 for (k = 0; k < nlist; k++)
481 init_fini_list[k]->init_flag &= ~DL_RESERVED;
484 _dl_free(local_scope);
486 /* Sort the INIT/FINI list in dependency order. */
487 for (runp2 = dep_list; runp2; runp2 = runp2->next) {
488 unsigned int j, k;
489 for (j = 0; init_fini_list[j] != runp2->tpnt; ++j)
490 /* Empty */;
491 for (k = j + 1; k < nlist; ++k) {
492 struct init_fini_list *ele = init_fini_list[k]->init_fini;
494 for (; ele; ele = ele->next) {
495 if (ele->tpnt == runp2->tpnt) {
496 struct elf_resolve *here = init_fini_list[k];
497 _dl_if_debug_print("Move %s from pos %d to %d in INIT/FINI list.\n", here->libname, k, j);
498 for (i = (k - j); i; --i)
499 init_fini_list[i+j] = init_fini_list[i+j-1];
500 init_fini_list[j] = here;
501 ++j;
502 break;
507 #ifdef __SUPPORT_LD_DEBUG__
508 if (_dl_debug) {
509 fprintf(stderr, "\nINIT/FINI order and dependencies:\n");
510 for (i = 0; i < nlist; i++) {
511 fprintf(stderr, "lib: %s has deps:\n", init_fini_list[i]->libname);
512 runp = init_fini_list[i]->init_fini;
513 for (; runp; runp = runp->next)
514 fprintf(stderr, " %s ", runp->tpnt->libname);
515 fprintf(stderr, "\n");
518 #endif
520 _dl_if_debug_print("Beginning dlopen relocation fixups\n");
522 * OK, now all of the kids are tucked into bed in their proper addresses.
523 * Now we go through and look for REL and RELA records that indicate fixups
524 * to the GOT tables. We need to do this in reverse order so that COPY
525 * directives work correctly */
527 #ifdef SHARED
529 * Get the tail of the list.
530 * In the static case doesn't need to extend the global scope, it is
531 * ready to be used as it is, because _dl_loaded_modules already points
532 * to the dlopened library.
534 for (ls = &_dl_loaded_modules->symbol_scope; ls && ls->next; ls = ls->next);
536 /* Extend the global scope by adding the local scope of the dlopened DSO. */
537 ls->next = &dyn_chain->dyn->symbol_scope;
538 #endif
539 #ifdef __mips__
541 * Relocation of the GOT entries for MIPS have to be done
542 * after all the libraries have been loaded.
544 _dl_perform_mips_global_got_relocations(tpnt, !now_flag);
545 #endif
547 if (_dl_fixup(dyn_chain, &_dl_loaded_modules->symbol_scope, now_flag))
548 goto oops;
550 if (relro_ptr) {
551 for (rpnt = relro_ptr->next; rpnt; rpnt = rpnt->next) {
552 if (rpnt->dyn->relro_size)
553 _dl_protect_relro(rpnt->dyn);
556 /* TODO: Should we set the protections of all pages back to R/O now ? */
559 #if defined(USE_TLS) && USE_TLS
561 for (i=0; i < nlist; i++) {
562 struct elf_resolve *tmp_tpnt = init_fini_list[i];
563 /* Only add TLS memory if this object is loaded now and
564 therefore is not yet initialized. */
566 if (!(tmp_tpnt->init_flag & INIT_FUNCS_CALLED)
567 /* Only if the module defines thread local data. */
568 && __builtin_expect (tmp_tpnt->l_tls_blocksize > 0, 0)) {
570 /* Now that we know the object is loaded successfully add
571 modules containing TLS data to the slot info table. We
572 might have to increase its size. */
573 _dl_add_to_slotinfo ((struct link_map*)tmp_tpnt);
575 /* It is the case in which we couldn't perform TLS static
576 initialization at relocation time, and we delayed it until
577 the relocation has been completed. */
579 if (tmp_tpnt->l_need_tls_init) {
580 tmp_tpnt->l_need_tls_init = 0;
581 # ifdef SHARED
582 /* Update the slot information data for at least the
583 generation of the DSO we are allocating data for. */
584 _dl_update_slotinfo (tmp_tpnt->l_tls_modid);
585 # endif
587 _dl_init_static_tls((struct link_map*)tmp_tpnt);
588 _dl_assert (tmp_tpnt->l_need_tls_init == 0);
591 /* We have to bump the generation counter. */
592 any_tls = true;
596 /* Bump the generation number if necessary. */
597 if (any_tls && __builtin_expect (++_dl_tls_generation == 0, 0)) {
598 _dl_debug_early("TLS generation counter wrapped! Please report this.");
599 _dl_exit(30);
602 #endif
604 /* Notify the debugger we have added some objects. */
605 if (_dl_debug_addr) {
606 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
607 if (dl_brk != NULL) {
608 _dl_debug_addr->r_state = RT_ADD;
609 (*dl_brk) ();
611 _dl_debug_addr->r_state = RT_CONSISTENT;
612 (*dl_brk) ();
616 /* Run the ctors and setup the dtors */
617 for (i = nlist; i; --i) {
618 tpnt = init_fini_list[i-1];
619 if (tpnt->init_flag & INIT_FUNCS_CALLED)
620 continue;
621 tpnt->init_flag |= INIT_FUNCS_CALLED;
623 if (tpnt->dynamic_info[DT_INIT]) {
624 void (*dl_elf_func) (void);
625 dl_elf_func = (void (*)(void)) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_INIT]);
626 if (dl_elf_func) {
627 _dl_if_debug_print("running ctors for library %s at '%p'\n",
628 tpnt->libname, dl_elf_func);
629 DL_CALL_FUNC_AT_ADDR (dl_elf_func, tpnt->loadaddr, (void(*)(void)));
633 _dl_run_init_array(tpnt);
636 _dl_unmap_cache();
637 return (void *) dyn_chain;
639 oops:
640 /* Something went wrong. Clean up and return NULL. */
641 _dl_unmap_cache();
642 do_dlclose(dyn_chain, 0);
643 return NULL;
646 void *dlopen(const char *libname, int flag)
648 void *ret;
650 __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex, 1);
651 ret = do_dlopen(libname, flag,
652 (ElfW(Addr)) __builtin_return_address(0));
653 __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex, 1);
655 return ret;
658 static void *do_dlsym(void *vhandle, const char *name, void *caller_address)
660 struct elf_resolve *tpnt, *tfrom;
661 struct dyn_elf *handle;
662 ElfW(Addr) from = 0;
663 struct dyn_elf *rpnt;
664 void *ret;
665 struct symbol_ref sym_ref = { NULL, NULL };
666 /* Nastiness to support underscore prefixes. */
667 #ifdef __UCLIBC_UNDERSCORES__
668 char tmp_buf[80];
669 char *name2 = tmp_buf;
670 size_t nlen = strlen (name) + 1;
671 if (nlen + 1 > sizeof (tmp_buf))
672 name2 = malloc (nlen + 1);
673 if (name2 == 0) {
674 _dl_error_number = LD_ERROR_MMAP_FAILED;
675 return 0;
677 name2[0] = '_';
678 memcpy (name2 + 1, name, nlen);
679 #else
680 const char *name2 = name;
681 #endif
682 handle = (struct dyn_elf *) vhandle;
684 /* First of all verify that we have a real handle
685 of some kind. Return NULL if not a valid handle. */
687 if (handle == NULL)
688 handle = _dl_symbol_tables;
689 else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
690 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
691 if (rpnt == handle)
692 break;
693 if (!rpnt) {
694 _dl_error_number = LD_BAD_HANDLE;
695 ret = NULL;
696 goto out;
698 } else if (handle == RTLD_NEXT) {
700 * Try and locate the module we were called from - we
701 * need this so that we know where to start searching
702 * from. We never pass RTLD_NEXT down into the actual
703 * dynamic loader itself, as it doesn't know
704 * how to properly treat it.
706 from = (ElfW(Addr)) caller_address;
708 tfrom = NULL;
709 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
710 tpnt = rpnt->dyn;
711 if (DL_ADDR_IN_LOADADDR(from, tpnt, tfrom)) {
712 tfrom = tpnt;
713 handle = rpnt->next;
717 tpnt = NULL;
718 if (handle == _dl_symbol_tables)
719 tpnt = handle->dyn; /* Only search RTLD_GLOBAL objs if global object */
721 do {
722 ret = _dl_find_hash(name2, &handle->dyn->symbol_scope, tpnt, ELF_RTYPE_CLASS_DLSYM, &sym_ref);
723 if (ret != NULL)
724 break;
725 handle = handle->next;
726 } while (from && handle);
728 #if defined(USE_TLS) && USE_TLS && defined SHARED
729 if (sym_ref.sym && (ELF_ST_TYPE(sym_ref.sym->st_info) == STT_TLS) && (sym_ref.tpnt)) {
730 /* The found symbol is a thread-local storage variable.
731 Return its address for the current thread. */
732 ret = _dl_tls_symaddr ((struct link_map *)sym_ref.tpnt, (Elf32_Addr)ret);
734 #endif
737 * Nothing found.
739 if (!ret)
740 _dl_error_number = LD_NO_SYMBOL;
741 out:
742 #ifdef __UCLIBC_UNDERSCORES__
743 if (name2 != tmp_buf)
744 free (name2);
745 #endif
746 return ret;
749 void *dlsym(void *vhandle, const char *name)
751 void *ret;
753 __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex, 1);
754 ret = do_dlsym(vhandle, name, __builtin_return_address(0));
755 __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex, 1);
757 return ret;
760 static int do_dlclose(void *vhandle, int need_fini)
762 struct dyn_elf *rpnt, *rpnt1, *rpnt1_tmp;
763 struct init_fini_list *runp, *tmp;
764 ElfW(Phdr) *ppnt;
765 struct elf_resolve *tpnt, *run_tpnt;
766 int (*dl_elf_fini) (void);
767 void (*dl_brk) (void);
768 struct dyn_elf *handle;
769 ElfW(Addr) end = 0, start = (ElfW(Addr))(~0ULL);
770 unsigned int i, j;
771 struct r_scope_elem *ls, *ls_next = NULL;
772 struct elf_resolve **handle_rlist;
774 #if defined(USE_TLS) && USE_TLS
775 bool any_tls = false;
776 size_t tls_free_start = NO_TLS_OFFSET;
777 size_t tls_free_end = NO_TLS_OFFSET;
778 struct link_map *tls_lmap;
779 #endif
781 handle = (struct dyn_elf *) vhandle;
782 if (handle == _dl_symbol_tables)
783 return 0;
784 rpnt1 = NULL;
785 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
786 if (rpnt == handle)
787 break;
788 rpnt1 = rpnt;
791 if (!rpnt) {
792 _dl_error_number = LD_BAD_HANDLE;
793 return 1;
795 if (rpnt1)
796 rpnt1->next_handle = rpnt->next_handle;
797 else
798 _dl_handles = rpnt->next_handle;
799 _dl_if_debug_print("%s: usage count: %d\n",
800 handle->dyn->libname, handle->dyn->usage_count);
801 if (handle->dyn->usage_count != 1) {
802 handle->dyn->usage_count--;
803 free(handle);
804 return 0;
807 /* Store the handle's local scope array for later removal */
808 handle_rlist = handle->dyn->symbol_scope.r_list;
810 /* Store references to the local scope entries for later removal */
811 for (ls = &_dl_loaded_modules->symbol_scope; ls && ls->next; ls = ls->next)
812 if (ls->next->r_list[0] == handle->dyn) {
813 break;
815 /* ls points to the previous local symbol scope */
816 if(ls && ls->next)
817 ls_next = ls->next->next;
819 /* OK, this is a valid handle - now close out the file */
820 for (j = 0; j < handle->init_fini.nlist; ++j) {
821 tpnt = handle->init_fini.init_fini[j];
822 tpnt->usage_count--;
823 if (tpnt->usage_count == 0) {
824 if ((tpnt->dynamic_info[DT_FINI]
825 || tpnt->dynamic_info[DT_FINI_ARRAY])
826 && need_fini
827 && !(tpnt->init_flag & FINI_FUNCS_CALLED)
829 tpnt->init_flag |= FINI_FUNCS_CALLED;
830 _dl_run_fini_array(tpnt);
832 if (tpnt->dynamic_info[DT_FINI]) {
833 dl_elf_fini = (int (*)(void)) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_FINI]);
834 _dl_if_debug_print("running dtors for library %s at '%p'\n",
835 tpnt->libname, dl_elf_fini);
836 DL_CALL_FUNC_AT_ADDR (dl_elf_fini, tpnt->loadaddr, (int (*)(void)));
840 _dl_if_debug_print("unmapping: %s\n", tpnt->libname);
841 end = 0;
842 for (i = 0, ppnt = tpnt->ppnt;
843 i < tpnt->n_phent; ppnt++, i++) {
844 if (ppnt->p_type != PT_LOAD)
845 continue;
846 if (ppnt->p_vaddr < start)
847 start = ppnt->p_vaddr;
848 if (end < ppnt->p_vaddr + ppnt->p_memsz)
849 end = ppnt->p_vaddr + ppnt->p_memsz;
852 #if defined(USE_TLS) && USE_TLS
853 /* Do the cast to make things easy. */
854 tls_lmap = (struct link_map *) tpnt;
856 /* Remove the object from the dtv slotinfo array if it uses TLS. */
857 if (__builtin_expect (tls_lmap->l_tls_blocksize > 0, 0)) {
858 any_tls = true;
860 if (_dl_tls_dtv_slotinfo_list != NULL
861 && ! remove_slotinfo (tls_lmap->l_tls_modid,
862 _dl_tls_dtv_slotinfo_list, 0,
863 (tpnt->init_flag & INIT_FUNCS_CALLED)))
864 /* All dynamically loaded modules with TLS are unloaded. */
865 _dl_tls_max_dtv_idx = _dl_tls_static_nelem;
867 if (tls_lmap->l_tls_offset != NO_TLS_OFFSET) {
869 * Collect a contiguous chunk built from the objects in
870 * this search list, going in either direction. When the
871 * whole chunk is at the end of the used area then we can
872 * reclaim it.
874 # if defined(TLS_TCB_AT_TP)
875 if (tls_free_start == NO_TLS_OFFSET
876 || (size_t) tls_lmap->l_tls_offset == tls_free_start) {
877 /* Extend the contiguous chunk being reclaimed. */
878 tls_free_start
879 = tls_lmap->l_tls_offset -
880 tls_lmap->l_tls_blocksize;
882 if (tls_free_end == NO_TLS_OFFSET)
883 tls_free_end = tls_lmap->l_tls_offset;
884 } else if (tls_lmap->l_tls_offset - tls_lmap->l_tls_blocksize
885 == tls_free_end)
886 /* Extend the chunk backwards. */
887 tls_free_end = tls_lmap->l_tls_offset;
888 else {
890 * This isn't contiguous with the last chunk freed.
891 * One of them will be leaked unless we can free
892 * one block right away.
894 if (tls_free_end == _dl_tls_static_used) {
895 _dl_tls_static_used = tls_free_start;
896 tls_free_end = tls_lmap->l_tls_offset;
897 tls_free_start
898 = tls_free_end - tls_lmap->l_tls_blocksize;
899 } else if ((size_t) tls_lmap->l_tls_offset
900 == _dl_tls_static_used)
901 _dl_tls_static_used = tls_lmap->l_tls_offset -
902 tls_lmap->l_tls_blocksize;
903 else if (tls_free_end < (size_t) tls_lmap->l_tls_offset) {
905 * We pick the later block. It has a chance
906 * to be freed.
908 tls_free_end = tls_lmap->l_tls_offset;
909 tls_free_start = tls_free_end -
910 tls_lmap->l_tls_blocksize;
913 # elif defined(TLS_DTV_AT_TP)
914 if ((size_t) tls_lmap->l_tls_offset == tls_free_end)
915 /* Extend the contiguous chunk being reclaimed. */
916 tls_free_end -= tls_lmap->l_tls_blocksize;
917 else if (tls_lmap->l_tls_offset + tls_lmap->l_tls_blocksize
918 == tls_free_start)
919 /* Extend the chunk backwards. */
920 tls_free_start = tls_lmap->l_tls_offset;
921 else {
923 * This isn't contiguous with the last chunk
924 * freed. One of them will be leaked.
926 if (tls_free_end == _dl_tls_static_used)
927 _dl_tls_static_used = tls_free_start;
928 tls_free_start = tls_lmap->l_tls_offset;
929 tls_free_end = tls_free_start +
930 tls_lmap->l_tls_blocksize;
932 # else
933 # error Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined
934 # endif
935 } else {
937 #define TLS_DTV_UNALLOCATED ((void *) -1l)
939 dtv_t *dtv = THREAD_DTV ();
941 if (!(dtv[tls_lmap->l_tls_modid].pointer.is_static) &&
942 dtv[tls_lmap->l_tls_modid].pointer.val != TLS_DTV_UNALLOCATED) {
943 /* Note that free is called for NULL is well. We
944 deallocate even if it is this dtv entry we are
945 supposed to load. The reason is that we call
946 memalign and not malloc. */
947 _dl_free (dtv[tls_lmap->l_tls_modid].pointer.val);
948 dtv[tls_lmap->l_tls_modid].pointer.val = TLS_DTV_UNALLOCATED;
952 #endif
954 end = (end + ADDR_ALIGN) & PAGE_ALIGN;
955 start = start & ~ADDR_ALIGN;
956 DL_LIB_UNMAP (tpnt, end - start);
957 /* Free elements in RTLD_LOCAL scope list */
958 for (runp = tpnt->rtld_local; runp; runp = tmp) {
959 tmp = runp->next;
960 free(runp);
963 /* Next, remove tpnt from the loaded_module list */
964 if (_dl_loaded_modules == tpnt) {
965 _dl_loaded_modules = tpnt->next;
966 if (_dl_loaded_modules)
967 _dl_loaded_modules->prev = 0;
968 } else {
969 for (run_tpnt = _dl_loaded_modules; run_tpnt; run_tpnt = run_tpnt->next) {
970 if (run_tpnt->next == tpnt) {
971 _dl_if_debug_print("removing loaded_modules: %s\n", tpnt->libname);
972 run_tpnt->next = run_tpnt->next->next;
973 if (run_tpnt->next)
974 run_tpnt->next->prev = run_tpnt;
975 break;
980 /* Next, remove tpnt from the global symbol table list */
981 if (_dl_symbol_tables) {
982 if (_dl_symbol_tables->dyn == tpnt) {
983 _dl_symbol_tables = _dl_symbol_tables->next;
984 if (_dl_symbol_tables)
985 _dl_symbol_tables->prev = 0;
986 } else {
987 for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
988 if (rpnt1->next->dyn == tpnt) {
989 _dl_if_debug_print("removing symbol_tables: %s\n", tpnt->libname);
990 rpnt1_tmp = rpnt1->next->next;
991 free(rpnt1->next);
992 rpnt1->next = rpnt1_tmp;
993 if (rpnt1->next)
994 rpnt1->next->prev = rpnt1;
995 break;
1000 free(tpnt->libname);
1001 if (handle->dyn != tpnt)
1002 free(tpnt->symbol_scope.r_list);
1003 free(tpnt);
1006 /* Unlink and release the handle's local scope from global one */
1007 if(ls)
1008 ls->next = ls_next;
1009 free(handle_rlist);
1011 for (rpnt1 = handle->next; rpnt1; rpnt1 = rpnt1_tmp) {
1012 rpnt1_tmp = rpnt1->next;
1013 free(rpnt1);
1015 free(handle->init_fini.init_fini);
1016 free(handle);
1018 #if defined(USE_TLS) && USE_TLS
1019 /* If we removed any object which uses TLS bump the generation counter. */
1020 if (any_tls) {
1021 if (__builtin_expect(++_dl_tls_generation == 0, 0)) {
1022 _dl_debug_early("TLS generation counter wrapped! Please report to the uClibc mailing list.\n");
1023 _dl_exit(30);
1026 if (tls_free_end == _dl_tls_static_used)
1027 _dl_tls_static_used = tls_free_start;
1029 #endif
1031 if (_dl_debug_addr) {
1032 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
1033 if (dl_brk != NULL) {
1034 _dl_debug_addr->r_state = RT_DELETE;
1035 (*dl_brk) ();
1037 _dl_debug_addr->r_state = RT_CONSISTENT;
1038 (*dl_brk) ();
1042 return 0;
1045 int dlclose(void *vhandle)
1047 int ret;
1049 __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex, 1);
1050 ret = do_dlclose(vhandle, 1);
1051 __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex, 1);
1053 return ret;
1056 char *dlerror(void)
1058 const char *retval;
1060 if (!_dl_error_number)
1061 return NULL;
1062 retval = dl_error_names[_dl_error_number];
1063 _dl_error_number = 0;
1064 return (char *)retval;
1067 #ifdef __USE_GNU
1068 static int do_dladdr(const void *__address, Dl_info * __info)
1070 struct elf_resolve *pelf;
1071 struct elf_resolve *rpnt;
1073 _dl_map_cache();
1076 * Try and locate the module address is in
1078 pelf = NULL;
1080 _dl_if_debug_print("__address: %p __info: %p\n", __address, __info);
1082 __address = DL_LOOKUP_ADDRESS (__address);
1084 for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
1085 struct elf_resolve *tpnt;
1087 tpnt = rpnt;
1089 _dl_if_debug_print("Module \"%s\" at %p\n",
1090 tpnt->libname, DL_LOADADDR_BASE(tpnt->loadaddr));
1092 if (DL_ADDR_IN_LOADADDR((ElfW(Addr)) __address, tpnt, pelf))
1093 pelf = tpnt;
1096 if (!pelf) {
1097 return 0;
1101 * Try and locate the symbol of address
1105 char *strtab;
1106 ElfW(Sym) *symtab;
1107 unsigned int hn, si, sn, sf;
1108 ElfW(Addr) sa = 0;
1110 /* Set the info for the object the address lies in */
1111 __info->dli_fname = pelf->libname;
1112 __info->dli_fbase = (void *)pelf->mapaddr;
1114 symtab = (ElfW(Sym) *) (pelf->dynamic_info[DT_SYMTAB]);
1115 strtab = (char *) (pelf->dynamic_info[DT_STRTAB]);
1117 sf = sn = 0;
1119 #ifdef __LDSO_GNU_HASH_SUPPORT__
1120 if (pelf->l_gnu_bitmask) {
1121 for (hn = 0; hn < pelf->nbucket; hn++) {
1122 si = pelf->l_gnu_buckets[hn];
1123 if (!si)
1124 continue;
1126 const Elf32_Word *hasharr = &pelf->l_gnu_chain_zero[si];
1127 do {
1128 ElfW(Addr) symbol_addr;
1130 symbol_addr = (ElfW(Addr)) DL_RELOC_ADDR(pelf->loadaddr, symtab[si].st_value);
1131 if ((symtab[si].st_shndx != SHN_UNDEF
1132 || symtab[si].st_value != 0)
1133 && ELF_ST_TYPE(symtab[si].st_info) != STT_TLS
1134 && DL_ADDR_SYM_MATCH(symbol_addr, &symtab[si], sa,
1135 (ElfW(Addr)) __address)) {
1136 sa = symbol_addr;
1137 sn = si;
1138 sf = 1;
1140 _dl_if_debug_print("Symbol \"%s\" at %p\n", strtab + symtab[si].st_name, symbol_addr);
1141 ++si;
1142 } while ((*hasharr++ & 1u) == 0);
1144 } else
1145 #endif
1146 for (hn = 0; hn < pelf->nbucket; hn++) {
1147 for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
1148 ElfW(Addr) symbol_addr;
1150 symbol_addr = (ElfW(Addr)) DL_RELOC_ADDR(pelf->loadaddr, symtab[si].st_value);
1151 if ((symtab[si].st_shndx != SHN_UNDEF
1152 || symtab[si].st_value != 0)
1153 && ELF_ST_TYPE(symtab[si].st_info) != STT_TLS
1154 && DL_ADDR_SYM_MATCH(symbol_addr, &symtab[si], sa,
1155 (ElfW(Addr)) __address)) {
1156 sa = symbol_addr;
1157 sn = si;
1158 sf = 1;
1161 _dl_if_debug_print("Symbol \"%s\" at %p\n",
1162 strtab + symtab[si].st_name, symbol_addr);
1166 if (sf) {
1167 /* A nearest symbol has been found; fill the entries */
1168 __info->dli_sname = strtab + symtab[sn].st_name;
1169 __info->dli_saddr = (void *)sa;
1170 } else {
1171 /* No symbol found, fill entries with NULL value,
1172 only the containing object will be returned. */
1173 __info->dli_sname = NULL;
1174 __info->dli_saddr = NULL;
1176 return 1;
1179 #endif
1181 int dladdr(const void *__address, Dl_info * __info)
1183 int ret;
1185 __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex, 1);
1186 ret = do_dladdr(__address, __info);
1187 __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex, 1);
1189 return ret;