libdl: remove LDSO_NO_CLEANUP
[uclibc-ng.git] / ldso / libdl / libdl.c
blob5c52f0687c1dfccaf092f3077868444b3ea5016a
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 #if 0
69 extern int _dl_fixup(struct dyn_elf *rpnt, struct r_scope_elem *scope, int lazy);
70 extern void _dl_protect_relro(struct elf_resolve * tpnt);
71 #endif
72 extern int _dl_errno;
73 extern struct dyn_elf *_dl_symbol_tables;
74 extern struct dyn_elf *_dl_handles;
75 extern struct elf_resolve *_dl_loaded_modules;
76 extern void _dl_free (void *__ptr);
77 extern struct r_debug *_dl_debug_addr;
78 extern unsigned long _dl_error_number;
79 extern void *(*_dl_malloc_function)(size_t);
80 extern void (*_dl_free_function) (void *p);
81 extern void _dl_run_init_array(struct elf_resolve *);
82 extern void _dl_run_fini_array(struct elf_resolve *);
83 #ifdef __LDSO_CACHE_SUPPORT__
84 int _dl_map_cache(void);
85 int _dl_unmap_cache(void);
86 #endif
87 #ifdef __mips__
88 extern void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt, int lazy);
89 #endif
90 #ifdef __SUPPORT_LD_DEBUG__
91 extern char *_dl_debug;
92 #endif
94 #else /* !SHARED */
96 /* When libdl is linked as a static library, we need to replace all
97 * the symbols that otherwise would have been loaded in from ldso... */
99 #ifdef __SUPPORT_LD_DEBUG__
100 char *_dl_debug = NULL;
101 char *_dl_debug_symbols = NULL;
102 char *_dl_debug_move = NULL;
103 char *_dl_debug_reloc = NULL;
104 char *_dl_debug_detail = NULL;
105 char *_dl_debug_nofixups = NULL;
106 char *_dl_debug_bindings = NULL;
107 int _dl_debug_file = 2;
108 #endif
109 const char *_dl_progname = ""; /* Program name */
110 void *(*_dl_malloc_function)(size_t);
111 void (*_dl_free_function) (void *p);
112 #ifdef __LDSO_LD_LIBRARY_PATH__
113 char *_dl_library_path = NULL; /* Where we look for libraries */
114 #endif
115 int _dl_errno = 0; /* We can't use the real errno in ldso */
116 size_t _dl_pagesize = PAGE_SIZE; /* Store the page size for use later */
117 /* This global variable is also to communicate with debuggers such as gdb. */
118 struct r_debug *_dl_debug_addr = NULL;
120 #include "../ldso/dl-array.c"
121 #include "../ldso/dl-debug.c"
124 # if defined(USE_TLS) && USE_TLS
126 * Giving this initialized value preallocates some surplus bytes in the
127 * static TLS area, see __libc_setup_tls (libc-tls.c).
129 size_t _dl_tls_static_size = 2048;
130 # endif
131 #include LDSO_ELFINTERP
132 #include "../ldso/dl-hash.c"
133 #define _dl_trace_loaded_objects 0
134 #include "../ldso/dl-elf.c"
135 #endif /* SHARED */
137 #ifdef __SUPPORT_LD_DEBUG__
138 # define _dl_if_debug_print(fmt, args...) \
139 do { \
140 if (_dl_debug) \
141 fprintf(stderr, "%s():%i: " fmt, __func__, __LINE__, ## args); \
142 } while (0)
143 #else
144 # define _dl_if_debug_print(fmt, args...)
145 #endif
147 static int do_dlclose(void *, int need_fini);
150 static const char *const dl_error_names[] = {
152 "File not found",
153 "Unable to open /dev/zero",
154 "Not an ELF file",
155 #if defined (__i386__)
156 "Not i386 binary",
157 #elif defined (__sparc__)
158 "Not sparc binary",
159 #elif defined (__mc68000__)
160 "Not m68k binary",
161 #else
162 "Unrecognized binary type",
163 #endif
164 "Not an ELF shared library",
165 "Unable to mmap file",
166 "No dynamic section",
167 "Library contains unsupported TLS",
168 #ifdef ELF_USES_RELOCA
169 "Unable to process REL relocs",
170 #else
171 "Unable to process RELA relocs",
172 #endif
173 "Bad handle",
174 "Unable to resolve symbol"
178 #if defined(USE_TLS) && USE_TLS
179 #ifdef SHARED
181 * Systems which do not have tls_index also probably have to define
182 * DONT_USE_TLS_INDEX.
185 # ifndef __TLS_GET_ADDR
186 # define __TLS_GET_ADDR __tls_get_addr
187 # endif
190 * Return the symbol address given the map of the module it is in and
191 * the symbol record. This is used in dl-sym.c.
193 static void *
194 internal_function
195 _dl_tls_symaddr(struct link_map *map, const Elf32_Addr st_value)
197 # ifndef DONT_USE_TLS_INDEX
198 tls_index tmp =
200 .ti_module = map->l_tls_modid,
201 .ti_offset = st_value
204 return __TLS_GET_ADDR (&tmp);
205 # else
206 return __TLS_GET_ADDR (map->l_tls_modid, st_value);
207 # endif
209 #endif
211 /* Returns true when a non-empty entry was found. */
212 static bool
213 remove_slotinfo(size_t idx, struct dtv_slotinfo_list *listp, size_t disp,
214 bool should_be_there)
216 if (idx - disp >= listp->len) {
217 if (listp->next == NULL) {
219 * The index is not actually valid in the slotinfo list,
220 * because this object was closed before it was fully set
221 * up due to some error.
223 _dl_assert(!should_be_there);
224 } else {
225 if (remove_slotinfo(idx, listp->next, disp + listp->len,
226 should_be_there))
227 return true;
230 * No non-empty entry. Search from the end of this element's
231 * slotinfo array.
233 idx = disp + listp->len;
235 } else {
236 struct link_map *old_map = listp->slotinfo[idx - disp].map;
239 * The entry might still be in its unused state if we are
240 * closing an object that wasn't fully set up.
242 if (__builtin_expect(old_map != NULL, 1)) {
243 _dl_assert(old_map->l_tls_modid == idx);
245 /* Mark the entry as unused. */
246 listp->slotinfo[idx - disp].gen = _dl_tls_generation + 1;
247 listp->slotinfo[idx - disp].map = NULL;
251 * If this is not the last currently used entry no need to
252 * look further.
254 if (idx != _dl_tls_max_dtv_idx)
255 return true;
258 while (idx - disp > (disp == 0 ? 1 + _dl_tls_static_nelem : 0)) {
259 --idx;
261 if (listp->slotinfo[idx - disp].map != NULL) {
262 /* Found a new last used index. */
263 _dl_tls_max_dtv_idx = idx;
264 return true;
268 /* No non-entry in this list element. */
269 return false;
271 #endif
273 static ptrdiff_t _dl_build_local_scope (struct elf_resolve **list,
274 struct elf_resolve *map)
276 struct elf_resolve **p = list;
277 struct init_fini_list *q;
279 *p++ = map;
280 map->init_flag |= DL_RESERVED;
281 if (map->init_fini)
282 for (q = map->init_fini; q; q = q->next)
283 if (! (q->tpnt->init_flag & DL_RESERVED))
284 p += _dl_build_local_scope (p, q->tpnt);
285 return p - list;
288 static void *do_dlopen(const char *libname, int flag, ElfW(Addr) from)
290 struct elf_resolve *tpnt, *tfrom;
291 struct dyn_elf *dyn_chain, *rpnt = NULL, *dyn_ptr, *relro_ptr, *handle;
292 struct elf_resolve *tpnt1;
293 void (*dl_brk) (void);
294 int now_flag;
295 struct init_fini_list *tmp, *runp, *runp2, *dep_list;
296 unsigned int nlist, i;
297 struct elf_resolve **init_fini_list;
298 static bool _dl_init;
299 struct elf_resolve **local_scope;
300 #ifdef SHARED
301 struct r_scope_elem *ls;
302 #endif
303 #if defined(USE_TLS) && USE_TLS
304 bool any_tls = false;
305 #endif
307 /* A bit of sanity checking... */
308 if (!(flag & (RTLD_LAZY|RTLD_NOW|RTLD_NOLOAD))) {
309 _dl_error_number = LD_BAD_HANDLE;
310 return NULL;
313 if (!_dl_init) {
314 _dl_init = true;
315 _dl_malloc_function = malloc;
316 _dl_free_function = free;
318 /* Cover the trivial case first */
319 if (!libname)
320 return _dl_symbol_tables;
322 #ifndef SHARED
323 # ifdef __SUPPORT_LD_DEBUG__
324 _dl_debug = getenv("LD_DEBUG");
325 if (_dl_debug) {
326 if (strstr(_dl_debug, "all")) {
327 _dl_debug_detail = _dl_debug_move = _dl_debug_symbols
328 = _dl_debug_reloc = _dl_debug_bindings = _dl_debug_nofixups = (void*)1;
329 } else {
330 _dl_debug_detail = strstr(_dl_debug, "detail");
331 _dl_debug_move = strstr(_dl_debug, "move");
332 _dl_debug_symbols = strstr(_dl_debug, "sym");
333 _dl_debug_reloc = strstr(_dl_debug, "reloc");
334 _dl_debug_nofixups = strstr(_dl_debug, "nofix");
335 _dl_debug_bindings = strstr(_dl_debug, "bind");
338 # endif
339 #endif
341 _dl_map_cache();
344 * Try and locate the module we were called from - we
345 * need this so that we get the correct RPATH/RUNPATH. Note that
346 * this is the current behavior under Solaris, but the
347 * ABI+ specifies that we should only use the RPATH from
348 * the application. Thus this may go away at some time
349 * in the future.
352 struct dyn_elf *dpnt;
353 tfrom = NULL;
354 for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
355 tpnt = dpnt->dyn;
356 if (DL_ADDR_IN_LOADADDR(from, tpnt, tfrom))
357 tfrom = tpnt;
360 for (rpnt = _dl_symbol_tables; rpnt && rpnt->next; rpnt = rpnt->next)
361 continue;
363 relro_ptr = rpnt;
364 now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
365 if (getenv("LD_BIND_NOW"))
366 now_flag = RTLD_NOW;
368 #if !defined SHARED && defined __LDSO_LD_LIBRARY_PATH__
369 /* When statically linked, the _dl_library_path is not yet initialized */
370 _dl_library_path = getenv("LD_LIBRARY_PATH");
371 #endif
373 /* Try to load the specified library */
374 _dl_if_debug_print("Trying to dlopen '%s', RTLD_GLOBAL:%d RTLD_NOW:%d\n",
375 (char*)libname, (flag & RTLD_GLOBAL ? 1:0), (now_flag & RTLD_NOW ? 1:0));
377 tpnt = _dl_load_shared_library(flag & (RTLD_NOLOAD | RTLD_GLOBAL | RTLD_NODELETE),
378 &rpnt, tfrom, (char*)libname, 0);
379 if (tpnt == NULL) {
380 _dl_unmap_cache();
381 return NULL;
383 dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
384 memset(dyn_chain, 0, sizeof(struct dyn_elf));
385 dyn_chain->dyn = tpnt;
387 dyn_chain->next_handle = _dl_handles;
388 _dl_handles = dyn_ptr = dyn_chain;
390 if (tpnt->init_flag & DL_OPENED2) {
391 _dl_if_debug_print("Lib: %s already opened\n", libname);
392 /* see if there is a handle from a earlier dlopen */
393 for (handle = _dl_handles->next_handle; handle; handle = handle->next_handle) {
394 if (handle->dyn == tpnt) {
395 dyn_chain->init_fini.init_fini = handle->init_fini.init_fini;
396 dyn_chain->init_fini.nlist = handle->init_fini.nlist;
397 for (i = 0; i < dyn_chain->init_fini.nlist; i++)
398 dyn_chain->init_fini.init_fini[i]->rtld_flags |= (flag & (RTLD_GLOBAL|RTLD_NODELETE));
399 dyn_chain->next = handle->next;
400 break;
403 return dyn_chain;
406 _dl_if_debug_print("Looking for needed libraries\n");
407 nlist = 0;
408 runp = alloca(sizeof(*runp));
409 runp->tpnt = tpnt;
410 runp->next = NULL;
411 dep_list = runp2 = runp;
412 for (; runp; runp = runp->next) {
413 ElfW(Dyn) *dpnt;
414 char *lpntstr;
416 nlist++;
417 runp->tpnt->init_fini = NULL; /* clear any previous dependcies */
418 for (dpnt = (ElfW(Dyn) *) runp->tpnt->dynamic_addr; dpnt->d_tag; dpnt++) {
419 if (dpnt->d_tag == DT_NEEDED) {
420 lpntstr = (char*) (runp->tpnt->dynamic_info[DT_STRTAB] +
421 dpnt->d_un.d_val);
422 _dl_if_debug_print("Trying to load '%s', needed by '%s'\n",
423 lpntstr, runp->tpnt->libname);
424 tpnt1 = _dl_load_shared_library(flag & (RTLD_GLOBAL | RTLD_NODELETE),
425 &rpnt, runp->tpnt, lpntstr, 0);
426 if (!tpnt1)
427 goto oops;
429 /* This list is for dlsym() and relocation */
430 dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
431 memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
432 dyn_ptr = dyn_ptr->next;
433 dyn_ptr->dyn = tpnt1;
434 /* Used to record RTLD_LOCAL scope */
435 tmp = alloca(sizeof(struct init_fini_list));
436 tmp->tpnt = tpnt1;
437 tmp->next = runp->tpnt->init_fini;
438 runp->tpnt->init_fini = tmp;
440 for (tmp=dep_list; tmp; tmp = tmp->next) {
441 if (tpnt1 == tmp->tpnt) { /* if match => cirular dependency, drop it */
442 _dl_if_debug_print("Circular dependency, skipping '%s',\n",
443 tmp->tpnt->libname);
444 tpnt1->usage_count--;
445 break;
448 if (!tmp) { /* Don't add if circular dependency detected */
449 runp2->next = alloca(sizeof(*runp));
450 runp2 = runp2->next;
451 runp2->tpnt = tpnt1;
452 runp2->next = NULL;
457 init_fini_list = malloc(nlist * sizeof(struct elf_resolve *));
458 dyn_chain->init_fini.init_fini = init_fini_list;
459 dyn_chain->init_fini.nlist = nlist;
460 i = 0;
461 for (runp2 = dep_list; runp2; runp2 = runp2->next) {
462 init_fini_list[i++] = runp2->tpnt;
463 for (runp = runp2->tpnt->init_fini; runp; runp = runp->next) {
464 if (!(runp->tpnt->rtld_flags & RTLD_GLOBAL)) {
465 tmp = malloc(sizeof(struct init_fini_list));
466 tmp->tpnt = runp->tpnt;
467 tmp->next = runp2->tpnt->rtld_local;
468 runp2->tpnt->rtld_local = tmp;
473 /* Build the local scope for the dynamically loaded modules. */
474 local_scope = _dl_malloc(nlist * sizeof(struct elf_resolve *)); /* Could it allocated on stack? */
475 for (i = 0; i < nlist; i++)
476 if (init_fini_list[i]->symbol_scope.r_nlist == 0) {
477 int k, cnt;
478 cnt = _dl_build_local_scope(local_scope, init_fini_list[i]);
479 init_fini_list[i]->symbol_scope.r_list = _dl_malloc(cnt * sizeof(struct elf_resolve *));
480 init_fini_list[i]->symbol_scope.r_nlist = cnt;
481 _dl_memcpy (init_fini_list[i]->symbol_scope.r_list, local_scope,
482 cnt * sizeof (struct elf_resolve *));
483 /* Restoring the init_flag.*/
484 for (k = 0; k < nlist; k++)
485 init_fini_list[k]->init_flag &= ~DL_RESERVED;
488 _dl_free(local_scope);
490 /* Sort the INIT/FINI list in dependency order. */
491 for (runp2 = dep_list; runp2; runp2 = runp2->next) {
492 unsigned int j, k;
493 for (j = 0; init_fini_list[j] != runp2->tpnt; ++j)
494 /* Empty */;
495 for (k = j + 1; k < nlist; ++k) {
496 struct init_fini_list *ele = init_fini_list[k]->init_fini;
498 for (; ele; ele = ele->next) {
499 if (ele->tpnt == runp2->tpnt) {
500 struct elf_resolve *here = init_fini_list[k];
501 _dl_if_debug_print("Move %s from pos %d to %d in INIT/FINI list.\n", here->libname, k, j);
502 for (i = (k - j); i; --i)
503 init_fini_list[i+j] = init_fini_list[i+j-1];
504 init_fini_list[j] = here;
505 ++j;
506 break;
511 #ifdef __SUPPORT_LD_DEBUG__
512 if (_dl_debug) {
513 fprintf(stderr, "\nINIT/FINI order and dependencies:\n");
514 for (i = 0; i < nlist; i++) {
515 fprintf(stderr, "lib: %s has deps:\n", init_fini_list[i]->libname);
516 runp = init_fini_list[i]->init_fini;
517 for (; runp; runp = runp->next)
518 fprintf(stderr, " %s ", runp->tpnt->libname);
519 fprintf(stderr, "\n");
522 #endif
524 _dl_if_debug_print("Beginning dlopen relocation fixups\n");
526 * OK, now all of the kids are tucked into bed in their proper addresses.
527 * Now we go through and look for REL and RELA records that indicate fixups
528 * to the GOT tables. We need to do this in reverse order so that COPY
529 * directives work correctly */
531 #ifdef SHARED
533 * Get the tail of the list.
534 * In the static case doesn't need to extend the global scope, it is
535 * ready to be used as it is, because _dl_loaded_modules already points
536 * to the dlopened library.
538 for (ls = &_dl_loaded_modules->symbol_scope; ls && ls->next; ls = ls->next);
540 /* Extend the global scope by adding the local scope of the dlopened DSO. */
541 ls->next = &dyn_chain->dyn->symbol_scope;
542 #endif
543 #ifdef __mips__
545 * Relocation of the GOT entries for MIPS have to be done
546 * after all the libraries have been loaded.
548 _dl_perform_mips_global_got_relocations(tpnt, !now_flag);
549 #endif
551 if (_dl_fixup(dyn_chain, &_dl_loaded_modules->symbol_scope, now_flag))
552 goto oops;
554 if (relro_ptr) {
555 for (rpnt = relro_ptr->next; rpnt; rpnt = rpnt->next) {
556 if (rpnt->dyn->relro_size)
557 _dl_protect_relro(rpnt->dyn);
560 /* TODO: Should we set the protections of all pages back to R/O now ? */
563 #if defined(USE_TLS) && USE_TLS
565 for (i=0; i < nlist; i++) {
566 struct elf_resolve *tmp_tpnt = init_fini_list[i];
567 /* Only add TLS memory if this object is loaded now and
568 therefore is not yet initialized. */
570 if (!(tmp_tpnt->init_flag & INIT_FUNCS_CALLED)
571 /* Only if the module defines thread local data. */
572 && __builtin_expect (tmp_tpnt->l_tls_blocksize > 0, 0)) {
574 /* Now that we know the object is loaded successfully add
575 modules containing TLS data to the slot info table. We
576 might have to increase its size. */
577 _dl_add_to_slotinfo ((struct link_map*)tmp_tpnt);
579 /* It is the case in which we couldn't perform TLS static
580 initialization at relocation time, and we delayed it until
581 the relocation has been completed. */
583 if (tmp_tpnt->l_need_tls_init) {
584 tmp_tpnt->l_need_tls_init = 0;
585 # ifdef SHARED
586 /* Update the slot information data for at least the
587 generation of the DSO we are allocating data for. */
588 _dl_update_slotinfo (tmp_tpnt->l_tls_modid);
589 # endif
591 _dl_init_static_tls((struct link_map*)tmp_tpnt);
592 _dl_assert (tmp_tpnt->l_need_tls_init == 0);
595 /* We have to bump the generation counter. */
596 any_tls = true;
600 /* Bump the generation number if necessary. */
601 if (any_tls && __builtin_expect (++_dl_tls_generation == 0, 0)) {
602 _dl_debug_early("TLS generation counter wrapped! Please report this.");
603 _dl_exit(30);
606 #endif
608 /* Notify the debugger we have added some objects. */
609 if (_dl_debug_addr) {
610 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
611 if (dl_brk != NULL) {
612 _dl_debug_addr->r_state = RT_ADD;
613 (*dl_brk) ();
615 _dl_debug_addr->r_state = RT_CONSISTENT;
616 (*dl_brk) ();
620 /* Run the ctors and setup the dtors */
621 for (i = nlist; i; --i) {
622 tpnt = init_fini_list[i-1];
623 if (tpnt->init_flag & INIT_FUNCS_CALLED)
624 continue;
625 tpnt->init_flag |= INIT_FUNCS_CALLED;
627 if (tpnt->dynamic_info[DT_INIT]) {
628 void (*dl_elf_func) (void);
629 dl_elf_func = (void (*)(void)) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_INIT]);
630 if (dl_elf_func) {
631 _dl_if_debug_print("running ctors for library %s at '%p'\n",
632 tpnt->libname, dl_elf_func);
633 DL_CALL_FUNC_AT_ADDR (dl_elf_func, tpnt->loadaddr, (void(*)(void)));
637 _dl_run_init_array(tpnt);
640 _dl_unmap_cache();
641 return (void *) dyn_chain;
643 oops:
644 /* Something went wrong. Clean up and return NULL. */
645 _dl_unmap_cache();
646 do_dlclose(dyn_chain, 0);
647 return NULL;
650 void *dlopen(const char *libname, int flag)
652 void *ret;
654 __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex, 1);
655 ret = do_dlopen(libname, flag,
656 (ElfW(Addr)) __builtin_return_address(0));
657 __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex, 1);
659 return ret;
662 static void *do_dlsym(void *vhandle, const char *name, void *caller_address)
664 struct elf_resolve *tpnt, *tfrom;
665 struct dyn_elf *handle;
666 ElfW(Addr) from = 0;
667 struct dyn_elf *rpnt;
668 void *ret;
669 struct symbol_ref sym_ref = { NULL, NULL };
670 /* Nastiness to support underscore prefixes. */
671 #ifdef __UCLIBC_UNDERSCORES__
672 char tmp_buf[80];
673 char *name2 = tmp_buf;
674 size_t nlen = strlen (name) + 1;
675 if (nlen + 1 > sizeof (tmp_buf))
676 name2 = malloc (nlen + 1);
677 if (name2 == 0) {
678 _dl_error_number = LD_ERROR_MMAP_FAILED;
679 return 0;
681 name2[0] = '_';
682 memcpy (name2 + 1, name, nlen);
683 #else
684 const char *name2 = name;
685 #endif
686 handle = (struct dyn_elf *) vhandle;
688 /* First of all verify that we have a real handle
689 of some kind. Return NULL if not a valid handle. */
691 if (handle == NULL)
692 handle = _dl_symbol_tables;
693 else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
694 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
695 if (rpnt == handle)
696 break;
697 if (!rpnt) {
698 _dl_error_number = LD_BAD_HANDLE;
699 ret = NULL;
700 goto out;
702 } else if (handle == RTLD_NEXT) {
704 * Try and locate the module we were called from - we
705 * need this so that we know where to start searching
706 * from. We never pass RTLD_NEXT down into the actual
707 * dynamic loader itself, as it doesn't know
708 * how to properly treat it.
710 from = (ElfW(Addr)) caller_address;
712 tfrom = NULL;
713 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
714 tpnt = rpnt->dyn;
715 if (DL_ADDR_IN_LOADADDR(from, tpnt, tfrom)) {
716 tfrom = tpnt;
717 handle = rpnt->next;
721 tpnt = NULL;
722 if (handle == _dl_symbol_tables)
723 tpnt = handle->dyn; /* Only search RTLD_GLOBAL objs if global object */
725 do {
726 ret = _dl_find_hash(name2, &handle->dyn->symbol_scope, tpnt, ELF_RTYPE_CLASS_DLSYM, &sym_ref);
727 if (ret != NULL)
728 break;
729 handle = handle->next;
730 } while (from && handle);
732 #if defined(USE_TLS) && USE_TLS && defined SHARED
733 if (sym_ref.sym && (ELF_ST_TYPE(sym_ref.sym->st_info) == STT_TLS) && (sym_ref.tpnt)) {
734 /* The found symbol is a thread-local storage variable.
735 Return its address for the current thread. */
736 ret = _dl_tls_symaddr ((struct link_map *)sym_ref.tpnt, (Elf32_Addr)ret);
738 #endif
741 * Nothing found.
743 if (!ret)
744 _dl_error_number = LD_NO_SYMBOL;
745 out:
746 #ifdef __UCLIBC_UNDERSCORES__
747 if (name2 != tmp_buf)
748 free (name2);
749 #endif
750 return ret;
753 void *dlsym(void *vhandle, const char *name)
755 void *ret;
757 __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex, 1);
758 ret = do_dlsym(vhandle, name, __builtin_return_address(0));
759 __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex, 1);
761 return ret;
764 #if 0
765 void *dlvsym(void *vhandle, const char *name, const char *version)
767 return dlsym(vhandle, name);
769 #endif
771 static int do_dlclose(void *vhandle, int need_fini)
773 struct dyn_elf *rpnt, *rpnt1, *rpnt1_tmp;
774 struct init_fini_list *runp, *tmp;
775 ElfW(Phdr) *ppnt;
776 struct elf_resolve *tpnt, *run_tpnt;
777 int (*dl_elf_fini) (void);
778 void (*dl_brk) (void);
779 struct dyn_elf *handle;
780 ElfW(Addr) end = 0, start = (ElfW(Addr))(~0ULL);
781 unsigned int i, j;
782 struct r_scope_elem *ls, *ls_next = NULL;
783 struct elf_resolve **handle_rlist;
785 #if defined(USE_TLS) && USE_TLS
786 bool any_tls = false;
787 size_t tls_free_start = NO_TLS_OFFSET;
788 size_t tls_free_end = NO_TLS_OFFSET;
789 struct link_map *tls_lmap;
790 #endif
792 handle = (struct dyn_elf *) vhandle;
793 if (handle == _dl_symbol_tables)
794 return 0;
795 rpnt1 = NULL;
796 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
797 if (rpnt == handle)
798 break;
799 rpnt1 = rpnt;
802 if (!rpnt) {
803 _dl_error_number = LD_BAD_HANDLE;
804 return 1;
806 if (rpnt1)
807 rpnt1->next_handle = rpnt->next_handle;
808 else
809 _dl_handles = rpnt->next_handle;
810 _dl_if_debug_print("%s: usage count: %d\n",
811 handle->dyn->libname, handle->dyn->usage_count);
812 if (handle->dyn->usage_count != 1) {
813 handle->dyn->usage_count--;
814 free(handle);
815 return 0;
818 /* Store the handle's local scope array for later removal */
819 handle_rlist = handle->dyn->symbol_scope.r_list;
821 /* Store references to the local scope entries for later removal */
822 for (ls = &_dl_loaded_modules->symbol_scope; ls && ls->next; ls = ls->next)
823 if (ls->next->r_list[0] == handle->dyn) {
824 break;
826 /* ls points to the previous local symbol scope */
827 if(ls && ls->next)
828 ls_next = ls->next->next;
830 /* OK, this is a valid handle - now close out the file */
831 for (j = 0; j < handle->init_fini.nlist; ++j) {
832 tpnt = handle->init_fini.init_fini[j];
833 tpnt->usage_count--;
834 if (tpnt->usage_count == 0) {
835 if ((tpnt->dynamic_info[DT_FINI]
836 || tpnt->dynamic_info[DT_FINI_ARRAY])
837 && need_fini
838 && !(tpnt->init_flag & FINI_FUNCS_CALLED)
840 tpnt->init_flag |= FINI_FUNCS_CALLED;
841 _dl_run_fini_array(tpnt);
843 if (tpnt->dynamic_info[DT_FINI]) {
844 dl_elf_fini = (int (*)(void)) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_FINI]);
845 _dl_if_debug_print("running dtors for library %s at '%p'\n",
846 tpnt->libname, dl_elf_fini);
847 DL_CALL_FUNC_AT_ADDR (dl_elf_fini, tpnt->loadaddr, (int (*)(void)));
851 _dl_if_debug_print("unmapping: %s\n", tpnt->libname);
852 end = 0;
853 for (i = 0, ppnt = tpnt->ppnt;
854 i < tpnt->n_phent; ppnt++, i++) {
855 if (ppnt->p_type != PT_LOAD)
856 continue;
857 if (ppnt->p_vaddr < start)
858 start = ppnt->p_vaddr;
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 end = (end + ADDR_ALIGN) & PAGE_ALIGN;
966 start = start & ~ADDR_ALIGN;
967 DL_LIB_UNMAP (tpnt, end - start);
968 /* Free elements in RTLD_LOCAL scope list */
969 for (runp = tpnt->rtld_local; runp; runp = tmp) {
970 tmp = runp->next;
971 free(runp);
974 /* Next, remove tpnt from the loaded_module list */
975 if (_dl_loaded_modules == tpnt) {
976 _dl_loaded_modules = tpnt->next;
977 if (_dl_loaded_modules)
978 _dl_loaded_modules->prev = 0;
979 } else {
980 for (run_tpnt = _dl_loaded_modules; run_tpnt; run_tpnt = run_tpnt->next) {
981 if (run_tpnt->next == tpnt) {
982 _dl_if_debug_print("removing loaded_modules: %s\n", tpnt->libname);
983 run_tpnt->next = run_tpnt->next->next;
984 if (run_tpnt->next)
985 run_tpnt->next->prev = run_tpnt;
986 break;
991 /* Next, remove tpnt from the global symbol table list */
992 if (_dl_symbol_tables) {
993 if (_dl_symbol_tables->dyn == tpnt) {
994 _dl_symbol_tables = _dl_symbol_tables->next;
995 if (_dl_symbol_tables)
996 _dl_symbol_tables->prev = 0;
997 } else {
998 for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
999 if (rpnt1->next->dyn == tpnt) {
1000 _dl_if_debug_print("removing symbol_tables: %s\n", tpnt->libname);
1001 rpnt1_tmp = rpnt1->next->next;
1002 free(rpnt1->next);
1003 rpnt1->next = rpnt1_tmp;
1004 if (rpnt1->next)
1005 rpnt1->next->prev = rpnt1;
1006 break;
1011 free(tpnt->libname);
1012 if (handle->dyn != tpnt)
1013 free(tpnt->symbol_scope.r_list);
1014 free(tpnt);
1017 /* Unlink and release the handle's local scope from global one */
1018 if(ls)
1019 ls->next = ls_next;
1020 free(handle_rlist);
1022 for (rpnt1 = handle->next; rpnt1; rpnt1 = rpnt1_tmp) {
1023 rpnt1_tmp = rpnt1->next;
1024 free(rpnt1);
1026 free(handle->init_fini.init_fini);
1027 free(handle);
1029 #if defined(USE_TLS) && USE_TLS
1030 /* If we removed any object which uses TLS bump the generation counter. */
1031 if (any_tls) {
1032 if (__builtin_expect(++_dl_tls_generation == 0, 0)) {
1033 _dl_debug_early("TLS generation counter wrapped! Please report to the uClibc mailing list.\n");
1034 _dl_exit(30);
1037 if (tls_free_end == _dl_tls_static_used)
1038 _dl_tls_static_used = tls_free_start;
1040 #endif
1042 if (_dl_debug_addr) {
1043 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
1044 if (dl_brk != NULL) {
1045 _dl_debug_addr->r_state = RT_DELETE;
1046 (*dl_brk) ();
1048 _dl_debug_addr->r_state = RT_CONSISTENT;
1049 (*dl_brk) ();
1053 return 0;
1056 int dlclose(void *vhandle)
1058 int ret;
1060 __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex, 1);
1061 ret = do_dlclose(vhandle, 1);
1062 __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex, 1);
1064 return ret;
1067 char *dlerror(void)
1069 const char *retval;
1071 if (!_dl_error_number)
1072 return NULL;
1073 retval = dl_error_names[_dl_error_number];
1074 _dl_error_number = 0;
1075 return (char *)retval;
1079 * Dump information to stderr about the current loaded modules
1081 #ifdef __USE_GNU
1082 # if 0
1083 static const char type[][4] = { "Lib", "Exe", "Int", "Mod" };
1085 /* reimplement this, being a GNU extension it should be the same as on glibc */
1086 int dlinfo(void)
1088 struct elf_resolve *tpnt;
1089 struct dyn_elf *rpnt, *hpnt;
1091 fprintf(stderr, "List of loaded modules\n");
1092 /* First start with a complete list of all of the loaded files. */
1093 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
1094 fprintf(stderr, "\t%p %p %p %s %d %s\n",
1095 DL_LOADADDR_BASE(tpnt->loadaddr), tpnt, tpnt->symbol_scope,
1096 type[tpnt->libtype],
1097 tpnt->usage_count, tpnt->libname);
1100 /* Next dump the module list for the application itself */
1101 fprintf(stderr, "\nModules for application (%p):\n", _dl_symbol_tables);
1102 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
1103 fprintf(stderr, "\t%p %s\n", rpnt->dyn, rpnt->dyn->libname);
1105 for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
1106 fprintf(stderr, "Modules for handle %p\n", hpnt);
1107 for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
1108 fprintf(stderr, "\t%p %s\n", rpnt->dyn, rpnt->dyn->libname);
1110 return 0;
1112 #endif
1114 static int do_dladdr(const void *__address, Dl_info * __info)
1116 struct elf_resolve *pelf;
1117 struct elf_resolve *rpnt;
1119 _dl_map_cache();
1122 * Try and locate the module address is in
1124 pelf = NULL;
1126 _dl_if_debug_print("__address: %p __info: %p\n", __address, __info);
1128 __address = DL_LOOKUP_ADDRESS (__address);
1130 for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
1131 struct elf_resolve *tpnt;
1133 tpnt = rpnt;
1135 _dl_if_debug_print("Module \"%s\" at %p\n",
1136 tpnt->libname, DL_LOADADDR_BASE(tpnt->loadaddr));
1138 if (DL_ADDR_IN_LOADADDR((ElfW(Addr)) __address, tpnt, pelf))
1139 pelf = tpnt;
1142 if (!pelf) {
1143 return 0;
1147 * Try and locate the symbol of address
1151 char *strtab;
1152 ElfW(Sym) *symtab;
1153 unsigned int hn, si, sn, sf;
1154 ElfW(Addr) sa = 0;
1156 /* Set the info for the object the address lies in */
1157 __info->dli_fname = pelf->libname;
1158 __info->dli_fbase = (void *)pelf->mapaddr;
1160 symtab = (ElfW(Sym) *) (pelf->dynamic_info[DT_SYMTAB]);
1161 strtab = (char *) (pelf->dynamic_info[DT_STRTAB]);
1163 sf = sn = 0;
1165 #ifdef __LDSO_GNU_HASH_SUPPORT__
1166 if (pelf->l_gnu_bitmask) {
1167 for (hn = 0; hn < pelf->nbucket; hn++) {
1168 si = pelf->l_gnu_buckets[hn];
1169 if (!si)
1170 continue;
1172 const Elf32_Word *hasharr = &pelf->l_gnu_chain_zero[si];
1173 do {
1174 ElfW(Addr) symbol_addr;
1176 symbol_addr = (ElfW(Addr)) DL_RELOC_ADDR(pelf->loadaddr, symtab[si].st_value);
1177 if ((symtab[si].st_shndx != SHN_UNDEF
1178 || symtab[si].st_value != 0)
1179 && ELF_ST_TYPE(symtab[si].st_info) != STT_TLS
1180 && DL_ADDR_SYM_MATCH(symbol_addr, &symtab[si], sa,
1181 (ElfW(Addr)) __address)) {
1182 sa = symbol_addr;
1183 sn = si;
1184 sf = 1;
1186 _dl_if_debug_print("Symbol \"%s\" at %p\n", strtab + symtab[si].st_name, symbol_addr);
1187 ++si;
1188 } while ((*hasharr++ & 1u) == 0);
1190 } else
1191 #endif
1192 for (hn = 0; hn < pelf->nbucket; hn++) {
1193 for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
1194 ElfW(Addr) symbol_addr;
1196 symbol_addr = (ElfW(Addr)) DL_RELOC_ADDR(pelf->loadaddr, symtab[si].st_value);
1197 if ((symtab[si].st_shndx != SHN_UNDEF
1198 || symtab[si].st_value != 0)
1199 && ELF_ST_TYPE(symtab[si].st_info) != STT_TLS
1200 && DL_ADDR_SYM_MATCH(symbol_addr, &symtab[si], sa,
1201 (ElfW(Addr)) __address)) {
1202 sa = symbol_addr;
1203 sn = si;
1204 sf = 1;
1207 _dl_if_debug_print("Symbol \"%s\" at %p\n",
1208 strtab + symtab[si].st_name, symbol_addr);
1212 if (sf) {
1213 /* A nearest symbol has been found; fill the entries */
1214 __info->dli_sname = strtab + symtab[sn].st_name;
1215 __info->dli_saddr = (void *)sa;
1216 } else {
1217 /* No symbol found, fill entries with NULL value,
1218 only the containing object will be returned. */
1219 __info->dli_sname = NULL;
1220 __info->dli_saddr = NULL;
1222 return 1;
1225 #endif
1227 int dladdr(const void *__address, Dl_info * __info)
1229 int ret;
1231 __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex, 1);
1232 ret = do_dladdr(__address, __info);
1233 __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex, 1);
1235 return ret;