1 /* vi: set sw=4 ts=4: */
3 * Program to load an ELF binary on a linux system, and run it
4 * after resolving ELF shared library symbols
6 * Copyright (C) 2000-2006 by Erik Andersen <andersen@uclibc.org>
7 * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
8 * David Engel, Hongjiu Lu and Mitch D'Souza
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. The name of the above contributors may not be
16 * used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <bits/uClibc_mutex.h>
39 #ifdef __UCLIBC_HAS_TLS__
43 #if defined(USE_TLS) && USE_TLS
46 extern void _dl_add_to_slotinfo(struct link_map
*l
);
49 /* TODO: get rid of global lock and use more finegrained locking, or
50 * perhaps RCU for the global structures */
51 __UCLIBC_MUTEX_STATIC(_dl_mutex
, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
);
54 # if defined(USE_TLS) && USE_TLS
55 extern struct link_map
*_dl_update_slotinfo(unsigned long int req_modid
);
58 /* When libdl is loaded as a shared library, we need to load in
59 * and use a pile of symbols from ldso... */
62 extern int _dl_fixup(struct dyn_elf
*rpnt
, struct r_scope_elem
*scope
, int lazy
);
63 extern void _dl_protect_relro(struct elf_resolve
* tpnt
);
66 extern struct dyn_elf
*_dl_symbol_tables
;
67 extern struct dyn_elf
*_dl_handles
;
68 extern struct elf_resolve
*_dl_loaded_modules
;
69 extern void _dl_free (void *__ptr
);
70 extern struct r_debug
*_dl_debug_addr
;
71 extern unsigned long _dl_error_number
;
72 extern void *(*_dl_malloc_function
)(size_t);
73 extern void (*_dl_free_function
) (void *p
);
74 extern void _dl_run_init_array(struct elf_resolve
*);
75 extern void _dl_run_fini_array(struct elf_resolve
*);
76 #ifdef __LDSO_CACHE_SUPPORT__
77 int _dl_map_cache(void);
78 int _dl_unmap_cache(void);
81 extern void _dl_perform_mips_global_got_relocations(struct elf_resolve
*tpnt
, int lazy
);
83 #ifdef __SUPPORT_LD_DEBUG__
84 extern char *_dl_debug
;
89 #define _dl_malloc malloc
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;
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 */
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;
127 #include LDSO_ELFINTERP
128 #include "../ldso/dl-hash.c"
129 #define _dl_trace_loaded_objects 0
130 #include "../ldso/dl-elf.c"
133 #ifdef __SUPPORT_LD_DEBUG__
134 # define _dl_if_debug_print(fmt, args...) \
137 fprintf(stderr, "%s():%i: " fmt, __func__, __LINE__, ## args); \
140 # define _dl_if_debug_print(fmt, args...)
143 static int do_dlclose(void *, int need_fini
);
146 static const char *const dl_error_names
[] = {
149 "Unable to open /dev/zero",
151 #if defined (__i386__)
153 #elif defined (__sparc__)
155 #elif defined (__mc68000__)
158 "Unrecognized binary type",
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",
167 "Unable to process RELA relocs",
170 "Unable to resolve symbol"
174 #if defined(USE_TLS) && USE_TLS
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
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.
191 _dl_tls_symaddr(struct link_map
*map
, const Elf32_Addr st_value
)
193 # ifndef DONT_USE_TLS_INDEX
196 .ti_module
= map
->l_tls_modid
,
197 .ti_offset
= st_value
200 return __TLS_GET_ADDR (&tmp
);
202 return __TLS_GET_ADDR (map
->l_tls_modid
, st_value
);
207 /* Returns true when a non-empty entry was found. */
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
);
221 if (remove_slotinfo(idx
, listp
->next
, disp
+ listp
->len
,
226 * No non-empty entry. Search from the end of this element's
229 idx
= disp
+ listp
->len
;
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
250 if (idx
!= _dl_tls_max_dtv_idx
)
254 while (idx
- disp
> (disp
== 0 ? 1 + _dl_tls_static_nelem
: 0)) {
257 if (listp
->slotinfo
[idx
- disp
].map
!= NULL
) {
258 /* Found a new last used index. */
259 _dl_tls_max_dtv_idx
= idx
;
264 /* No non-entry in this list element. */
269 #ifndef __LDSO_NO_CLEANUP__
270 void dl_cleanup(void) attribute_hidden
__attribute__ ((destructor
));
271 void dl_cleanup(void)
273 struct dyn_elf
*h
, *n
;
275 for (h
= _dl_handles
; h
; h
= n
) {
282 static ptrdiff_t _dl_build_local_scope (struct elf_resolve
**list
,
283 struct elf_resolve
*map
)
285 struct elf_resolve
**p
= list
;
286 struct init_fini_list
*q
;
289 map
->init_flag
|= DL_RESERVED
;
291 for (q
= map
->init_fini
; q
; q
= q
->next
)
292 if (! (q
->tpnt
->init_flag
& DL_RESERVED
))
293 p
+= _dl_build_local_scope (p
, q
->tpnt
);
297 static void *do_dlopen(const char *libname
, int flag
, ElfW(Addr
) from
)
299 struct elf_resolve
*tpnt
, *tfrom
;
300 struct dyn_elf
*dyn_chain
, *rpnt
= NULL
, *dyn_ptr
, *relro_ptr
, *handle
;
301 struct elf_resolve
*tpnt1
;
302 void (*dl_brk
) (void);
304 struct init_fini_list
*tmp
, *runp
, *runp2
, *dep_list
;
305 unsigned int nlist
, i
;
306 struct elf_resolve
**init_fini_list
;
307 static bool _dl_init
;
308 struct elf_resolve
**local_scope
;
310 struct r_scope_elem
*ls
;
312 #if defined(USE_TLS) && USE_TLS
313 bool any_tls
= false;
316 /* A bit of sanity checking... */
317 if (!(flag
& (RTLD_LAZY
|RTLD_NOW
|RTLD_NOLOAD
))) {
318 _dl_error_number
= LD_BAD_HANDLE
;
324 _dl_malloc_function
= malloc
;
325 _dl_free_function
= free
;
327 /* Cover the trivial case first */
329 return _dl_symbol_tables
;
332 # ifdef __SUPPORT_LD_DEBUG__
333 _dl_debug
= getenv("LD_DEBUG");
335 if (strstr(_dl_debug
, "all")) {
336 _dl_debug_detail
= _dl_debug_move
= _dl_debug_symbols
337 = _dl_debug_reloc
= _dl_debug_bindings
= _dl_debug_nofixups
= (void*)1;
339 _dl_debug_detail
= strstr(_dl_debug
, "detail");
340 _dl_debug_move
= strstr(_dl_debug
, "move");
341 _dl_debug_symbols
= strstr(_dl_debug
, "sym");
342 _dl_debug_reloc
= strstr(_dl_debug
, "reloc");
343 _dl_debug_nofixups
= strstr(_dl_debug
, "nofix");
344 _dl_debug_bindings
= strstr(_dl_debug
, "bind");
353 * Try and locate the module we were called from - we
354 * need this so that we get the correct RPATH/RUNPATH. Note that
355 * this is the current behavior under Solaris, but the
356 * ABI+ specifies that we should only use the RPATH from
357 * the application. Thus this may go away at some time
361 struct dyn_elf
*dpnt
;
363 for (dpnt
= _dl_symbol_tables
; dpnt
; dpnt
= dpnt
->next
) {
365 if (DL_ADDR_IN_LOADADDR(from
, tpnt
, tfrom
))
369 for (rpnt
= _dl_symbol_tables
; rpnt
&& rpnt
->next
; rpnt
= rpnt
->next
)
373 now_flag
= (flag
& RTLD_NOW
) ? RTLD_NOW
: 0;
374 if (getenv("LD_BIND_NOW"))
377 #if !defined SHARED && defined __LDSO_LD_LIBRARY_PATH__
378 /* When statically linked, the _dl_library_path is not yet initialized */
379 _dl_library_path
= getenv("LD_LIBRARY_PATH");
382 /* Try to load the specified library */
383 _dl_if_debug_print("Trying to dlopen '%s', RTLD_GLOBAL:%d RTLD_NOW:%d\n",
384 (char*)libname
, (flag
& RTLD_GLOBAL
? 1:0), (now_flag
& RTLD_NOW
? 1:0));
386 tpnt
= _dl_load_shared_library(flag
& (RTLD_NOLOAD
| RTLD_GLOBAL
| RTLD_NODELETE
),
387 &rpnt
, tfrom
, (char*)libname
, 0);
392 dyn_chain
= (struct dyn_elf
*) malloc(sizeof(struct dyn_elf
));
393 memset(dyn_chain
, 0, sizeof(struct dyn_elf
));
394 dyn_chain
->dyn
= tpnt
;
396 dyn_chain
->next_handle
= _dl_handles
;
397 _dl_handles
= dyn_ptr
= dyn_chain
;
399 if (tpnt
->init_flag
& DL_OPENED2
) {
400 _dl_if_debug_print("Lib: %s already opened\n", libname
);
401 /* see if there is a handle from a earlier dlopen */
402 for (handle
= _dl_handles
->next_handle
; handle
; handle
= handle
->next_handle
) {
403 if (handle
->dyn
== tpnt
) {
404 dyn_chain
->init_fini
.init_fini
= handle
->init_fini
.init_fini
;
405 dyn_chain
->init_fini
.nlist
= handle
->init_fini
.nlist
;
406 for (i
= 0; i
< dyn_chain
->init_fini
.nlist
; i
++)
407 dyn_chain
->init_fini
.init_fini
[i
]->rtld_flags
|= (flag
& (RTLD_GLOBAL
|RTLD_NODELETE
));
408 dyn_chain
->next
= handle
->next
;
415 _dl_if_debug_print("Looking for needed libraries\n");
417 runp
= alloca(sizeof(*runp
));
420 dep_list
= runp2
= runp
;
421 for (; runp
; runp
= runp
->next
) {
426 runp
->tpnt
->init_fini
= NULL
; /* clear any previous dependcies */
427 for (dpnt
= (ElfW(Dyn
) *) runp
->tpnt
->dynamic_addr
; dpnt
->d_tag
; dpnt
++) {
428 if (dpnt
->d_tag
== DT_NEEDED
) {
429 lpntstr
= (char*) (runp
->tpnt
->dynamic_info
[DT_STRTAB
] +
431 _dl_if_debug_print("Trying to load '%s', needed by '%s'\n",
432 lpntstr
, runp
->tpnt
->libname
);
433 tpnt1
= _dl_load_shared_library(flag
& (RTLD_GLOBAL
| RTLD_NODELETE
),
434 &rpnt
, runp
->tpnt
, lpntstr
, 0);
438 /* This list is for dlsym() and relocation */
439 dyn_ptr
->next
= (struct dyn_elf
*) malloc(sizeof(struct dyn_elf
));
440 memset (dyn_ptr
->next
, 0, sizeof (struct dyn_elf
));
441 dyn_ptr
= dyn_ptr
->next
;
442 dyn_ptr
->dyn
= tpnt1
;
443 /* Used to record RTLD_LOCAL scope */
444 tmp
= alloca(sizeof(struct init_fini_list
));
446 tmp
->next
= runp
->tpnt
->init_fini
;
447 runp
->tpnt
->init_fini
= tmp
;
449 for (tmp
=dep_list
; tmp
; tmp
= tmp
->next
) {
450 if (tpnt1
== tmp
->tpnt
) { /* if match => cirular dependency, drop it */
451 _dl_if_debug_print("Circular dependency, skipping '%s',\n",
453 tpnt1
->usage_count
--;
457 if (!tmp
) { /* Don't add if circular dependency detected */
458 runp2
->next
= alloca(sizeof(*runp
));
466 init_fini_list
= malloc(nlist
* sizeof(struct elf_resolve
*));
467 dyn_chain
->init_fini
.init_fini
= init_fini_list
;
468 dyn_chain
->init_fini
.nlist
= nlist
;
470 for (runp2
= dep_list
; runp2
; runp2
= runp2
->next
) {
471 init_fini_list
[i
++] = runp2
->tpnt
;
472 for (runp
= runp2
->tpnt
->init_fini
; runp
; runp
= runp
->next
) {
473 if (!(runp
->tpnt
->rtld_flags
& RTLD_GLOBAL
)) {
474 tmp
= malloc(sizeof(struct init_fini_list
));
475 tmp
->tpnt
= runp
->tpnt
;
476 tmp
->next
= runp2
->tpnt
->rtld_local
;
477 runp2
->tpnt
->rtld_local
= tmp
;
482 /* Build the local scope for the dynamically loaded modules. */
483 local_scope
= _dl_malloc(nlist
* sizeof(struct elf_resolve
*)); /* Could it allocated on stack? */
484 for (i
= 0; i
< nlist
; i
++)
485 if (init_fini_list
[i
]->symbol_scope
.r_nlist
== 0) {
487 cnt
= _dl_build_local_scope(local_scope
, init_fini_list
[i
]);
488 init_fini_list
[i
]->symbol_scope
.r_list
= _dl_malloc(cnt
* sizeof(struct elf_resolve
*));
489 init_fini_list
[i
]->symbol_scope
.r_nlist
= cnt
;
490 _dl_memcpy (init_fini_list
[i
]->symbol_scope
.r_list
, local_scope
,
491 cnt
* sizeof (struct elf_resolve
*));
492 /* Restoring the init_flag.*/
493 for (k
= 0; k
< nlist
; k
++)
494 init_fini_list
[k
]->init_flag
&= ~DL_RESERVED
;
497 _dl_free(local_scope
);
499 /* Sort the INIT/FINI list in dependency order. */
500 for (runp2
= dep_list
; runp2
; runp2
= runp2
->next
) {
502 for (j
= 0; init_fini_list
[j
] != runp2
->tpnt
; ++j
)
504 for (k
= j
+ 1; k
< nlist
; ++k
) {
505 struct init_fini_list
*ele
= init_fini_list
[k
]->init_fini
;
507 for (; ele
; ele
= ele
->next
) {
508 if (ele
->tpnt
== runp2
->tpnt
) {
509 struct elf_resolve
*here
= init_fini_list
[k
];
510 _dl_if_debug_print("Move %s from pos %d to %d in INIT/FINI list.\n", here
->libname
, k
, j
);
511 for (i
= (k
- j
); i
; --i
)
512 init_fini_list
[i
+j
] = init_fini_list
[i
+j
-1];
513 init_fini_list
[j
] = here
;
520 #ifdef __SUPPORT_LD_DEBUG__
522 fprintf(stderr
, "\nINIT/FINI order and dependencies:\n");
523 for (i
= 0; i
< nlist
; i
++) {
524 fprintf(stderr
, "lib: %s has deps:\n", init_fini_list
[i
]->libname
);
525 runp
= init_fini_list
[i
]->init_fini
;
526 for (; runp
; runp
= runp
->next
)
527 fprintf(stderr
, " %s ", runp
->tpnt
->libname
);
528 fprintf(stderr
, "\n");
533 _dl_if_debug_print("Beginning dlopen relocation fixups\n");
535 * OK, now all of the kids are tucked into bed in their proper addresses.
536 * Now we go through and look for REL and RELA records that indicate fixups
537 * to the GOT tables. We need to do this in reverse order so that COPY
538 * directives work correctly */
542 * Get the tail of the list.
543 * In the static case doesn't need to extend the global scope, it is
544 * ready to be used as it is, because _dl_loaded_modules already points
545 * to the dlopened library.
547 for (ls
= &_dl_loaded_modules
->symbol_scope
; ls
&& ls
->next
; ls
= ls
->next
);
549 /* Extend the global scope by adding the local scope of the dlopened DSO. */
550 ls
->next
= &dyn_chain
->dyn
->symbol_scope
;
554 * Relocation of the GOT entries for MIPS have to be done
555 * after all the libraries have been loaded.
557 _dl_perform_mips_global_got_relocations(tpnt
, !now_flag
);
560 if (_dl_fixup(dyn_chain
, &_dl_loaded_modules
->symbol_scope
, now_flag
))
564 for (rpnt
= relro_ptr
->next
; rpnt
; rpnt
= rpnt
->next
) {
565 if (rpnt
->dyn
->relro_size
)
566 _dl_protect_relro(rpnt
->dyn
);
569 /* TODO: Should we set the protections of all pages back to R/O now ? */
572 #if defined(USE_TLS) && USE_TLS
574 for (i
=0; i
< nlist
; i
++) {
575 struct elf_resolve
*tmp_tpnt
= init_fini_list
[i
];
576 /* Only add TLS memory if this object is loaded now and
577 therefore is not yet initialized. */
579 if (!(tmp_tpnt
->init_flag
& INIT_FUNCS_CALLED
)
580 /* Only if the module defines thread local data. */
581 && __builtin_expect (tmp_tpnt
->l_tls_blocksize
> 0, 0)) {
583 /* Now that we know the object is loaded successfully add
584 modules containing TLS data to the slot info table. We
585 might have to increase its size. */
586 _dl_add_to_slotinfo ((struct link_map
*)tmp_tpnt
);
588 /* It is the case in which we couldn't perform TLS static
589 initialization at relocation time, and we delayed it until
590 the relocation has been completed. */
592 if (tmp_tpnt
->l_need_tls_init
) {
593 tmp_tpnt
->l_need_tls_init
= 0;
595 /* Update the slot information data for at least the
596 generation of the DSO we are allocating data for. */
597 _dl_update_slotinfo (tmp_tpnt
->l_tls_modid
);
600 _dl_init_static_tls((struct link_map
*)tmp_tpnt
);
601 _dl_assert (tmp_tpnt
->l_need_tls_init
== 0);
604 /* We have to bump the generation counter. */
609 /* Bump the generation number if necessary. */
610 if (any_tls
&& __builtin_expect (++_dl_tls_generation
== 0, 0)) {
611 _dl_debug_early("TLS generation counter wrapped! Please report this.");
617 /* Notify the debugger we have added some objects. */
618 if (_dl_debug_addr
) {
619 dl_brk
= (void (*)(void)) _dl_debug_addr
->r_brk
;
620 if (dl_brk
!= NULL
) {
621 _dl_debug_addr
->r_state
= RT_ADD
;
624 _dl_debug_addr
->r_state
= RT_CONSISTENT
;
629 /* Run the ctors and setup the dtors */
630 for (i
= nlist
; i
; --i
) {
631 tpnt
= init_fini_list
[i
-1];
632 if (tpnt
->init_flag
& INIT_FUNCS_CALLED
)
634 tpnt
->init_flag
|= INIT_FUNCS_CALLED
;
636 if (tpnt
->dynamic_info
[DT_INIT
]) {
637 void (*dl_elf_func
) (void);
638 dl_elf_func
= (void (*)(void)) DL_RELOC_ADDR(tpnt
->loadaddr
, tpnt
->dynamic_info
[DT_INIT
]);
640 _dl_if_debug_print("running ctors for library %s at '%p'\n",
641 tpnt
->libname
, dl_elf_func
);
642 DL_CALL_FUNC_AT_ADDR (dl_elf_func
, tpnt
->loadaddr
, (void(*)(void)));
646 _dl_run_init_array(tpnt
);
650 return (void *) dyn_chain
;
653 /* Something went wrong. Clean up and return NULL. */
655 do_dlclose(dyn_chain
, 0);
659 void *dlopen(const char *libname
, int flag
)
663 __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex
, 1);
664 ret
= do_dlopen(libname
, flag
,
665 (ElfW(Addr
)) __builtin_return_address(0));
666 __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex
, 1);
671 static void *do_dlsym(void *vhandle
, const char *name
, void *caller_address
)
673 struct elf_resolve
*tpnt
, *tfrom
;
674 struct dyn_elf
*handle
;
676 struct dyn_elf
*rpnt
;
678 struct symbol_ref sym_ref
= { NULL
, NULL
};
679 /* Nastiness to support underscore prefixes. */
680 #ifdef __UCLIBC_UNDERSCORES__
682 char *name2
= tmp_buf
;
683 size_t nlen
= strlen (name
) + 1;
684 if (nlen
+ 1 > sizeof (tmp_buf
))
685 name2
= malloc (nlen
+ 1);
687 _dl_error_number
= LD_ERROR_MMAP_FAILED
;
691 memcpy (name2
+ 1, name
, nlen
);
693 const char *name2
= name
;
695 handle
= (struct dyn_elf
*) vhandle
;
697 /* First of all verify that we have a real handle
698 of some kind. Return NULL if not a valid handle. */
701 handle
= _dl_symbol_tables
;
702 else if (handle
!= RTLD_NEXT
&& handle
!= _dl_symbol_tables
) {
703 for (rpnt
= _dl_handles
; rpnt
; rpnt
= rpnt
->next_handle
)
707 _dl_error_number
= LD_BAD_HANDLE
;
711 } else if (handle
== RTLD_NEXT
) {
713 * Try and locate the module we were called from - we
714 * need this so that we know where to start searching
715 * from. We never pass RTLD_NEXT down into the actual
716 * dynamic loader itself, as it doesn't know
717 * how to properly treat it.
719 from
= (ElfW(Addr
)) caller_address
;
722 for (rpnt
= _dl_symbol_tables
; rpnt
; rpnt
= rpnt
->next
) {
724 if (DL_ADDR_IN_LOADADDR(from
, tpnt
, tfrom
)) {
731 if (handle
== _dl_symbol_tables
)
732 tpnt
= handle
->dyn
; /* Only search RTLD_GLOBAL objs if global object */
735 ret
= _dl_find_hash(name2
, &handle
->dyn
->symbol_scope
, tpnt
, ELF_RTYPE_CLASS_DLSYM
, &sym_ref
);
738 handle
= handle
->next
;
739 } while (from
&& handle
);
741 #if defined(USE_TLS) && USE_TLS && defined SHARED
742 if (sym_ref
.sym
&& (ELF_ST_TYPE(sym_ref
.sym
->st_info
) == STT_TLS
) && (sym_ref
.tpnt
)) {
743 /* The found symbol is a thread-local storage variable.
744 Return its address for the current thread. */
745 ret
= _dl_tls_symaddr ((struct link_map
*)sym_ref
.tpnt
, (Elf32_Addr
)ret
);
753 _dl_error_number
= LD_NO_SYMBOL
;
755 #ifdef __UCLIBC_UNDERSCORES__
756 if (name2
!= tmp_buf
)
762 void *dlsym(void *vhandle
, const char *name
)
766 __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex
, 1);
767 ret
= do_dlsym(vhandle
, name
, __builtin_return_address(0));
768 __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex
, 1);
774 void *dlvsym(void *vhandle
, const char *name
, const char *version
)
776 return dlsym(vhandle
, name
);
780 static int do_dlclose(void *vhandle
, int need_fini
)
782 struct dyn_elf
*rpnt
, *rpnt1
, *rpnt1_tmp
;
783 struct init_fini_list
*runp
, *tmp
;
785 struct elf_resolve
*tpnt
, *run_tpnt
;
786 int (*dl_elf_fini
) (void);
787 void (*dl_brk
) (void);
788 struct dyn_elf
*handle
;
789 unsigned int end
= 0, start
= 0xffffffff;
791 struct r_scope_elem
*ls
, *ls_next
= NULL
;
792 struct elf_resolve
**handle_rlist
;
794 #if defined(USE_TLS) && USE_TLS
795 bool any_tls
= false;
796 size_t tls_free_start
= NO_TLS_OFFSET
;
797 size_t tls_free_end
= NO_TLS_OFFSET
;
798 struct link_map
*tls_lmap
;
801 handle
= (struct dyn_elf
*) vhandle
;
802 if (handle
== _dl_symbol_tables
)
805 for (rpnt
= _dl_handles
; rpnt
; rpnt
= rpnt
->next_handle
) {
812 _dl_error_number
= LD_BAD_HANDLE
;
816 rpnt1
->next_handle
= rpnt
->next_handle
;
818 _dl_handles
= rpnt
->next_handle
;
819 _dl_if_debug_print("%s: usage count: %d\n",
820 handle
->dyn
->libname
, handle
->dyn
->usage_count
);
821 if (handle
->dyn
->usage_count
!= 1 || (handle
->dyn
->rtld_flags
& RTLD_NODELETE
)) {
822 handle
->dyn
->usage_count
--;
827 /* Store the handle's local scope array for later removal */
828 handle_rlist
= handle
->dyn
->symbol_scope
.r_list
;
830 /* Store references to the local scope entries for later removal */
831 for (ls
= &_dl_loaded_modules
->symbol_scope
; ls
&& ls
->next
; ls
= ls
->next
)
832 if (ls
->next
->r_list
[0] == handle
->dyn
) {
835 /* ls points to the previous local symbol scope */
837 ls_next
= ls
->next
->next
;
839 /* OK, this is a valid handle - now close out the file */
840 for (j
= 0; j
< handle
->init_fini
.nlist
; ++j
) {
841 tpnt
= handle
->init_fini
.init_fini
[j
];
843 if (tpnt
->usage_count
== 0 && !(tpnt
->rtld_flags
& RTLD_NODELETE
)) {
844 if ((tpnt
->dynamic_info
[DT_FINI
]
845 || tpnt
->dynamic_info
[DT_FINI_ARRAY
])
847 && !(tpnt
->init_flag
& FINI_FUNCS_CALLED
)
849 tpnt
->init_flag
|= FINI_FUNCS_CALLED
;
850 _dl_run_fini_array(tpnt
);
852 if (tpnt
->dynamic_info
[DT_FINI
]) {
853 dl_elf_fini
= (int (*)(void)) DL_RELOC_ADDR(tpnt
->loadaddr
, tpnt
->dynamic_info
[DT_FINI
]);
854 _dl_if_debug_print("running dtors for library %s at '%p'\n",
855 tpnt
->libname
, dl_elf_fini
);
856 DL_CALL_FUNC_AT_ADDR (dl_elf_fini
, tpnt
->loadaddr
, (int (*)(void)));
860 _dl_if_debug_print("unmapping: %s\n", tpnt
->libname
);
862 for (i
= 0, ppnt
= tpnt
->ppnt
;
863 i
< tpnt
->n_phent
; ppnt
++, i
++) {
864 if (ppnt
->p_type
!= PT_LOAD
)
866 if (ppnt
->p_vaddr
< start
)
867 start
= ppnt
->p_vaddr
;
868 if (end
< ppnt
->p_vaddr
+ ppnt
->p_memsz
)
869 end
= ppnt
->p_vaddr
+ ppnt
->p_memsz
;
872 #if defined(USE_TLS) && USE_TLS
873 /* Do the cast to make things easy. */
874 tls_lmap
= (struct link_map
*) tpnt
;
876 /* Remove the object from the dtv slotinfo array if it uses TLS. */
877 if (__builtin_expect (tls_lmap
->l_tls_blocksize
> 0, 0)) {
880 if (_dl_tls_dtv_slotinfo_list
!= NULL
881 && ! remove_slotinfo (tls_lmap
->l_tls_modid
,
882 _dl_tls_dtv_slotinfo_list
, 0,
883 (tpnt
->init_flag
& INIT_FUNCS_CALLED
)))
884 /* All dynamically loaded modules with TLS are unloaded. */
885 _dl_tls_max_dtv_idx
= _dl_tls_static_nelem
;
887 if (tls_lmap
->l_tls_offset
!= NO_TLS_OFFSET
) {
889 * Collect a contiguous chunk built from the objects in
890 * this search list, going in either direction. When the
891 * whole chunk is at the end of the used area then we can
894 # if defined(TLS_TCB_AT_TP)
895 if (tls_free_start
== NO_TLS_OFFSET
896 || (size_t) tls_lmap
->l_tls_offset
== tls_free_start
) {
897 /* Extend the contiguous chunk being reclaimed. */
899 = tls_lmap
->l_tls_offset
-
900 tls_lmap
->l_tls_blocksize
;
902 if (tls_free_end
== NO_TLS_OFFSET
)
903 tls_free_end
= tls_lmap
->l_tls_offset
;
904 } else if (tls_lmap
->l_tls_offset
- tls_lmap
->l_tls_blocksize
906 /* Extend the chunk backwards. */
907 tls_free_end
= tls_lmap
->l_tls_offset
;
910 * This isn't contiguous with the last chunk freed.
911 * One of them will be leaked unless we can free
912 * one block right away.
914 if (tls_free_end
== _dl_tls_static_used
) {
915 _dl_tls_static_used
= tls_free_start
;
916 tls_free_end
= tls_lmap
->l_tls_offset
;
918 = tls_free_end
- tls_lmap
->l_tls_blocksize
;
919 } else if ((size_t) tls_lmap
->l_tls_offset
920 == _dl_tls_static_used
)
921 _dl_tls_static_used
= tls_lmap
->l_tls_offset
-
922 tls_lmap
->l_tls_blocksize
;
923 else if (tls_free_end
< (size_t) tls_lmap
->l_tls_offset
) {
925 * We pick the later block. It has a chance
928 tls_free_end
= tls_lmap
->l_tls_offset
;
929 tls_free_start
= tls_free_end
-
930 tls_lmap
->l_tls_blocksize
;
933 # elif defined(TLS_DTV_AT_TP)
934 if ((size_t) tls_lmap
->l_tls_offset
== tls_free_end
)
935 /* Extend the contiguous chunk being reclaimed. */
936 tls_free_end
-= tls_lmap
->l_tls_blocksize
;
937 else if (tls_lmap
->l_tls_offset
+ tls_lmap
->l_tls_blocksize
939 /* Extend the chunk backwards. */
940 tls_free_start
= tls_lmap
->l_tls_offset
;
943 * This isn't contiguous with the last chunk
944 * freed. One of them will be leaked.
946 if (tls_free_end
== _dl_tls_static_used
)
947 _dl_tls_static_used
= tls_free_start
;
948 tls_free_start
= tls_lmap
->l_tls_offset
;
949 tls_free_end
= tls_free_start
+
950 tls_lmap
->l_tls_blocksize
;
953 # error Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined
957 #define TLS_DTV_UNALLOCATED ((void *) -1l)
959 dtv_t
*dtv
= THREAD_DTV ();
961 if (!(dtv
[tls_lmap
->l_tls_modid
].pointer
.is_static
) &&
962 dtv
[tls_lmap
->l_tls_modid
].pointer
.val
!= TLS_DTV_UNALLOCATED
) {
963 /* Note that free is called for NULL is well. We
964 deallocate even if it is this dtv entry we are
965 supposed to load. The reason is that we call
966 memalign and not malloc. */
967 _dl_free (dtv
[tls_lmap
->l_tls_modid
].pointer
.val
);
968 dtv
[tls_lmap
->l_tls_modid
].pointer
.val
= TLS_DTV_UNALLOCATED
;
974 end
= (end
+ ADDR_ALIGN
) & PAGE_ALIGN
;
975 start
= start
& ~ADDR_ALIGN
;
976 DL_LIB_UNMAP (tpnt
, end
- start
);
977 /* Free elements in RTLD_LOCAL scope list */
978 for (runp
= tpnt
->rtld_local
; runp
; runp
= tmp
) {
983 /* Next, remove tpnt from the loaded_module list */
984 if (_dl_loaded_modules
== tpnt
) {
985 _dl_loaded_modules
= tpnt
->next
;
986 if (_dl_loaded_modules
)
987 _dl_loaded_modules
->prev
= 0;
989 for (run_tpnt
= _dl_loaded_modules
; run_tpnt
; run_tpnt
= run_tpnt
->next
) {
990 if (run_tpnt
->next
== tpnt
) {
991 _dl_if_debug_print("removing loaded_modules: %s\n", tpnt
->libname
);
992 run_tpnt
->next
= run_tpnt
->next
->next
;
994 run_tpnt
->next
->prev
= run_tpnt
;
1000 /* Next, remove tpnt from the global symbol table list */
1001 if (_dl_symbol_tables
) {
1002 if (_dl_symbol_tables
->dyn
== tpnt
) {
1003 _dl_symbol_tables
= _dl_symbol_tables
->next
;
1004 if (_dl_symbol_tables
)
1005 _dl_symbol_tables
->prev
= 0;
1007 for (rpnt1
= _dl_symbol_tables
; rpnt1
->next
; rpnt1
= rpnt1
->next
) {
1008 if (rpnt1
->next
->dyn
== tpnt
) {
1009 _dl_if_debug_print("removing symbol_tables: %s\n", tpnt
->libname
);
1010 rpnt1_tmp
= rpnt1
->next
->next
;
1012 rpnt1
->next
= rpnt1_tmp
;
1014 rpnt1
->next
->prev
= rpnt1
;
1020 free(tpnt
->libname
);
1021 if (handle
->dyn
!= tpnt
)
1022 free(tpnt
->symbol_scope
.r_list
);
1026 /* Unlink and release the handle's local scope from global one */
1031 for (rpnt1
= handle
->next
; rpnt1
; rpnt1
= rpnt1_tmp
) {
1032 rpnt1_tmp
= rpnt1
->next
;
1035 free(handle
->init_fini
.init_fini
);
1038 #if defined(USE_TLS) && USE_TLS
1039 /* If we removed any object which uses TLS bump the generation counter. */
1041 if (__builtin_expect(++_dl_tls_generation
== 0, 0)) {
1042 _dl_debug_early("TLS generation counter wrapped! Please report to the uClibc mailing list.\n");
1046 if (tls_free_end
== _dl_tls_static_used
)
1047 _dl_tls_static_used
= tls_free_start
;
1051 if (_dl_debug_addr
) {
1052 dl_brk
= (void (*)(void)) _dl_debug_addr
->r_brk
;
1053 if (dl_brk
!= NULL
) {
1054 _dl_debug_addr
->r_state
= RT_DELETE
;
1057 _dl_debug_addr
->r_state
= RT_CONSISTENT
;
1065 int dlclose(void *vhandle
)
1069 __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex
, 1);
1070 ret
= do_dlclose(vhandle
, 1);
1071 __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex
, 1);
1080 if (!_dl_error_number
)
1082 retval
= dl_error_names
[_dl_error_number
];
1083 _dl_error_number
= 0;
1084 return (char *)retval
;
1088 * Dump information to stderr about the current loaded modules
1092 static const char type
[][4] = { "Lib", "Exe", "Int", "Mod" };
1094 /* reimplement this, being a GNU extension it should be the same as on glibc */
1097 struct elf_resolve
*tpnt
;
1098 struct dyn_elf
*rpnt
, *hpnt
;
1100 fprintf(stderr
, "List of loaded modules\n");
1101 /* First start with a complete list of all of the loaded files. */
1102 for (tpnt
= _dl_loaded_modules
; tpnt
; tpnt
= tpnt
->next
) {
1103 fprintf(stderr
, "\t%p %p %p %s %d %s\n",
1104 DL_LOADADDR_BASE(tpnt
->loadaddr
), tpnt
, tpnt
->symbol_scope
,
1105 type
[tpnt
->libtype
],
1106 tpnt
->usage_count
, tpnt
->libname
);
1109 /* Next dump the module list for the application itself */
1110 fprintf(stderr
, "\nModules for application (%p):\n", _dl_symbol_tables
);
1111 for (rpnt
= _dl_symbol_tables
; rpnt
; rpnt
= rpnt
->next
)
1112 fprintf(stderr
, "\t%p %s\n", rpnt
->dyn
, rpnt
->dyn
->libname
);
1114 for (hpnt
= _dl_handles
; hpnt
; hpnt
= hpnt
->next_handle
) {
1115 fprintf(stderr
, "Modules for handle %p\n", hpnt
);
1116 for (rpnt
= hpnt
; rpnt
; rpnt
= rpnt
->next
)
1117 fprintf(stderr
, "\t%p %s\n", rpnt
->dyn
, rpnt
->dyn
->libname
);
1123 static int do_dladdr(const void *__address
, Dl_info
* __info
)
1125 struct elf_resolve
*pelf
;
1126 struct elf_resolve
*rpnt
;
1131 * Try and locate the module address is in
1135 _dl_if_debug_print("__address: %p __info: %p\n", __address
, __info
);
1137 __address
= DL_LOOKUP_ADDRESS (__address
);
1139 for (rpnt
= _dl_loaded_modules
; rpnt
; rpnt
= rpnt
->next
) {
1140 struct elf_resolve
*tpnt
;
1144 _dl_if_debug_print("Module \"%s\" at %p\n",
1145 tpnt
->libname
, DL_LOADADDR_BASE(tpnt
->loadaddr
));
1147 if (DL_ADDR_IN_LOADADDR((ElfW(Addr
)) __address
, tpnt
, pelf
))
1156 * Try and locate the symbol of address
1162 unsigned int hn
, si
, sn
, sf
;
1165 /* Set the info for the object the address lies in */
1166 __info
->dli_fname
= pelf
->libname
;
1167 __info
->dli_fbase
= (void *)pelf
->mapaddr
;
1169 symtab
= (ElfW(Sym
) *) (pelf
->dynamic_info
[DT_SYMTAB
]);
1170 strtab
= (char *) (pelf
->dynamic_info
[DT_STRTAB
]);
1174 #ifdef __LDSO_GNU_HASH_SUPPORT__
1175 if (pelf
->l_gnu_bitmask
) {
1176 for (hn
= 0; hn
< pelf
->nbucket
; hn
++) {
1177 si
= pelf
->l_gnu_buckets
[hn
];
1181 const Elf32_Word
*hasharr
= &pelf
->l_gnu_chain_zero
[si
];
1183 ElfW(Addr
) symbol_addr
;
1185 symbol_addr
= (ElfW(Addr
)) DL_RELOC_ADDR(pelf
->loadaddr
, symtab
[si
].st_value
);
1186 if ((symtab
[si
].st_shndx
!= SHN_UNDEF
1187 || symtab
[si
].st_value
!= 0)
1188 && ELF_ST_TYPE(symtab
[si
].st_info
) != STT_TLS
1189 && DL_ADDR_SYM_MATCH(symbol_addr
, &symtab
[si
], sa
,
1190 (ElfW(Addr
)) __address
)) {
1195 _dl_if_debug_print("Symbol \"%s\" at %p\n", strtab
+ symtab
[si
].st_name
, symbol_addr
);
1197 } while ((*hasharr
++ & 1u) == 0);
1201 for (hn
= 0; hn
< pelf
->nbucket
; hn
++) {
1202 for (si
= pelf
->elf_buckets
[hn
]; si
; si
= pelf
->chains
[si
]) {
1203 ElfW(Addr
) symbol_addr
;
1205 symbol_addr
= (ElfW(Addr
)) DL_RELOC_ADDR(pelf
->loadaddr
, symtab
[si
].st_value
);
1206 if ((symtab
[si
].st_shndx
!= SHN_UNDEF
1207 || symtab
[si
].st_value
!= 0)
1208 && ELF_ST_TYPE(symtab
[si
].st_info
) != STT_TLS
1209 && DL_ADDR_SYM_MATCH(symbol_addr
, &symtab
[si
], sa
,
1210 (ElfW(Addr
)) __address
)) {
1216 _dl_if_debug_print("Symbol \"%s\" at %p\n",
1217 strtab
+ symtab
[si
].st_name
, symbol_addr
);
1222 /* A nearest symbol has been found; fill the entries */
1223 __info
->dli_sname
= strtab
+ symtab
[sn
].st_name
;
1224 __info
->dli_saddr
= (void *)sa
;
1226 /* No symbol found, fill entries with NULL value,
1227 only the containing object will be returned. */
1228 __info
->dli_sname
= NULL
;
1229 __info
->dli_saddr
= NULL
;
1236 int dladdr(const void *__address
, Dl_info
* __info
)
1240 __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex
, 1);
1241 ret
= do_dladdr(__address
, __info
);
1242 __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex
, 1);