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-2004 by Erik Andersen <andersen@codepoet.org>
7 * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
8 * David Engel, Hongjiu Lu and Mitch D'Souza
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
38 #if defined (__LIBDL_SHARED__)
40 /* When libdl is loaded as a shared library, we need to load in
41 * and use a pile of symbols from ldso... */
43 extern char *_dl_find_hash(const char *, struct dyn_elf *, struct elf_resolve *, int)
44 __attribute__ ((__weak__));
45 extern struct elf_resolve * _dl_load_shared_library(int, struct dyn_elf **,
46 struct elf_resolve *, char *, int) __attribute__ ((__weak__));
47 extern struct elf_resolve * _dl_check_if_named_library_is_loaded(const char *, int)
48 __attribute__ ((__weak__));
49 extern int _dl_fixup(struct dyn_elf *rpnt, int lazy)
50 __attribute__ ((__weak__));
51 extern void _dl_protect_relro(struct elf_resolve * tpnt)
52 __attribute__ ((__weak__));
53 extern int _dl_errno __attribute__ ((__weak__));
54 extern struct dyn_elf *_dl_symbol_tables __attribute__ ((__weak__));
55 extern struct dyn_elf *_dl_handles __attribute__ ((__weak__));
56 extern struct elf_resolve *_dl_loaded_modules __attribute__ ((__weak__));
57 extern struct r_debug *_dl_debug_addr __attribute__ ((__weak__));
58 extern unsigned long _dl_error_number __attribute__ ((__weak__));
59 extern void *(*_dl_malloc_function)(size_t) __attribute__ ((__weak__));
60 #ifdef __LDSO_CACHE_SUPPORT__
61 int _dl_map_cache(void) __attribute__ ((__weak__));
62 int _dl_unmap_cache(void) __attribute__ ((__weak__));
65 extern void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt, int lazy)
66 __attribute__ ((__weak__));
68 #ifdef __SUPPORT_LD_DEBUG__
69 extern char *_dl_debug __attribute__ ((__weak__));
73 #else /* __LIBDL_SHARED__ */
75 /* When libdl is linked as a static library, we need to replace all
76 * the symbols that otherwise would have been loaded in from ldso... */
78 #ifdef __SUPPORT_LD_DEBUG__
81 char *_dl_library_path = 0; /* Where we look for libraries */
82 char *_dl_ldsopath = 0; /* Location of the shared lib loader */
83 int _dl_errno = 0; /* We can't use the real errno in ldso */
84 size_t _dl_pagesize = PAGE_SIZE; /* Store the page size for use later */
85 /* This global variable is also to communicate with debuggers such as gdb. */
86 struct r_debug *_dl_debug_addr = NULL;
87 #define _dl_malloc malloc
88 #include "dl-progname.h"
89 #include "../ldso/dl-hash.c"
90 #define _dl_trace_loaded_objects 0
91 #include "../ldso/dl-elf.c"
94 static int do_dlclose(void *, int need_fini);
97 static const char *dl_error_names[] = {
100 "Unable to open /dev/zero",
102 #if defined (__i386__)
104 #elif defined (__sparc__)
106 #elif defined (__mc68000__)
109 "Unrecognized binary type",
111 "Not an ELF shared library",
112 "Unable to mmap file",
113 "No dynamic section",
114 #ifdef ELF_USES_RELOCA
115 "Unable to process REL relocs",
117 "Unable to process RELA relocs",
120 "Unable to resolve symbol"
123 void __attribute__ ((destructor)) dl_cleanup(void)
126 for (d = _dl_handles; d; d = d->next_handle) {
131 void *dlopen(const char *libname, int flag)
133 struct elf_resolve *tpnt, *tfrom, *tcurr=NULL;
134 struct dyn_elf *dyn_chain, *rpnt = NULL, *dyn_ptr, *relro_ptr, *handle;
135 struct dyn_elf *dpnt;
137 struct elf_resolve *tpnt1;
138 void (*dl_brk) (void);
140 struct init_fini_list *tmp, *runp, *runp2, *dep_list;
142 struct elf_resolve **init_fini_list;
144 /* A bit of sanity checking... */
145 if (!(flag & (RTLD_LAZY|RTLD_NOW))) {
146 _dl_error_number = LD_BAD_HANDLE;
150 from = (ElfW(Addr)) __builtin_return_address(0);
152 /* Cover the trivial case first */
154 return _dl_symbol_tables;
159 * Try and locate the module we were called from - we
160 * need this so that we get the correct RPATH. Note that
161 * this is the current behavior under Solaris, but the
162 * ABI+ specifies that we should only use the RPATH from
163 * the application. Thus this may go away at some time
167 for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
169 if (tpnt->loadaddr < from
170 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr))
173 for(rpnt = _dl_symbol_tables; rpnt->next; rpnt=rpnt->next);
176 /* Try to load the specified library */
177 #ifdef __SUPPORT_LD_DEBUG__
179 fprintf(stderr, "Trying to dlopen '%s'\n", (char*)libname);
181 tpnt = _dl_check_if_named_library_is_loaded((char *)libname, 0);
183 tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname, 0);
190 dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
191 _dl_memset(dyn_chain, 0, sizeof(struct dyn_elf));
192 dyn_chain->dyn = tpnt;
193 tpnt->rtld_flags |= (flag & RTLD_GLOBAL);
195 dyn_chain->next_handle = _dl_handles;
196 _dl_handles = dyn_ptr = dyn_chain;
198 if (tpnt->usage_count > 1) {
199 #ifdef __SUPPORT_LD_DEBUG__
201 fprintf(stderr, "Lib: % already opened\n", libname);
203 /* see if there is a handle from a earlier dlopen */
204 for (handle = _dl_handles->next_handle; handle; handle = handle->next_handle) {
205 if (handle->dyn == tpnt) {
206 dyn_chain->init_fini.init_fini = handle->init_fini.init_fini;
207 dyn_chain->init_fini.nlist = handle->init_fini.nlist;
208 for(i=0; i < dyn_chain->init_fini.nlist; i++)
209 dyn_chain->init_fini.init_fini[i]->rtld_flags |= (flag & RTLD_GLOBAL);
210 dyn_chain->next = handle->next;
216 tpnt->init_flag |= DL_OPENED;
219 #ifdef __SUPPORT_LD_DEBUG__
221 fprintf(stderr, "Looking for needed libraries\n");
224 runp = alloca(sizeof(*runp));
227 dep_list = runp2 = runp;
228 for (; runp; runp = runp->next)
234 runp->tpnt->init_fini = NULL; /* clear any previous dependcies */
235 for (dpnt = (Elf32_Dyn *) runp->tpnt->dynamic_addr; dpnt->d_tag; dpnt++) {
236 if (dpnt->d_tag == DT_NEEDED) {
239 lpntstr = (char*) (runp->tpnt->dynamic_info[DT_STRTAB] +
241 name = _dl_get_last_path_component(lpntstr);
242 tpnt1 = _dl_check_if_named_library_is_loaded(name, 0);
243 #ifdef __SUPPORT_LD_DEBUG__
245 fprintf(stderr, "Trying to load '%s', needed by '%s'\n",
246 lpntstr, runp->tpnt->libname);
249 tpnt1->usage_count++;
251 tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpntstr, 0);
254 tpnt1->init_flag |= DL_OPENED;
257 tpnt1->rtld_flags |= (flag & RTLD_GLOBAL);
259 if (tpnt1->usage_count == 1) {
260 /* This list is for dlsym() and relocation */
261 dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
262 _dl_memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
263 dyn_ptr = dyn_ptr->next;
264 dyn_ptr->dyn = tpnt1;
266 if (tpnt1->init_flag & DL_OPENED) {
267 /* Used to record RTLD_LOCAL scope */
268 tmp = alloca(sizeof(struct init_fini_list)); /* Allocates on stack, no need to free this memory */
270 tmp->next = runp->tpnt->init_fini;
271 runp->tpnt->init_fini = tmp;
273 runp2->next = alloca(sizeof(*runp)); /* Allocates on stack, no need to free this memory */
281 init_fini_list = malloc(nlist * sizeof(struct elf_resolve *));
282 dyn_chain->init_fini.init_fini = init_fini_list;
283 dyn_chain->init_fini.nlist = nlist;
285 for (runp2 = dep_list; runp2; runp2 = runp2->next) {
286 init_fini_list[i++] = runp2->tpnt;
287 for(runp = runp2->tpnt->init_fini; runp; runp = runp->next){
288 if (!(runp->tpnt->rtld_flags & RTLD_GLOBAL)) {
289 tmp = malloc(sizeof(struct init_fini_list));
290 tmp->tpnt = runp->tpnt;
291 tmp->next = runp2->tpnt->rtld_local;
292 runp2->tpnt->rtld_local = tmp;
297 /* Sort the INIT/FINI list in dependency order. */
298 for (runp2 = dep_list; runp2; runp2 = runp2->next) {
300 for (j = 0; init_fini_list[j] != runp2->tpnt; ++j)
302 for (k = j + 1; k < nlist; ++k) {
303 struct init_fini_list *runp = init_fini_list[k]->init_fini;
305 for (; runp; runp = runp->next) {
306 if (runp->tpnt == runp2->tpnt) {
307 struct elf_resolve *here = init_fini_list[k];
308 #ifdef __SUPPORT_LD_DEBUG__
310 fprintf(stderr, "Move %s from pos %d to %d in INIT/FINI list.\n", here->libname, k, j);
312 for (i = (k - j); i; --i)
313 init_fini_list[i+j] = init_fini_list[i+j-1];
314 init_fini_list[j] = here;
321 #ifdef __SUPPORT_LD_DEBUG__
323 fprintf(stderr, "\nINIT/FINI order and dependencies:\n");
324 for (i=0;i < nlist;i++) {
325 fprintf(stderr, "lib: %s has deps:\n", init_fini_list[i]->libname);
326 runp = init_fini_list[i]->init_fini;
327 for ( ;runp; runp = runp->next)
328 printf(" %s ", runp->tpnt->libname);
334 #ifdef __SUPPORT_LD_DEBUG__
336 fprintf(stderr, "Beginning dlopen relocation fixups\n");
339 * OK, now all of the kids are tucked into bed in their proper addresses.
340 * Now we go through and look for REL and RELA records that indicate fixups
341 * to the GOT tables. We need to do this in reverse order so that COPY
342 * directives work correctly */
343 now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
344 if (getenv("LD_BIND_NOW"))
349 * Relocation of the GOT entries for MIPS have to be done
350 * after all the libraries have been loaded.
352 _dl_perform_mips_global_got_relocations(tpnt, !now_flag);
355 if (_dl_fixup(dyn_chain, now_flag))
358 for (rpnt = relro_ptr->next; rpnt; rpnt = rpnt->next) {
359 if (rpnt->dyn->relro_size)
360 _dl_protect_relro(rpnt->dyn);
362 /* TODO: Should we set the protections of all pages back to R/O now ? */
365 /* Notify the debugger we have added some objects. */
366 if (_dl_debug_addr) {
367 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
368 if (dl_brk != NULL) {
369 _dl_debug_addr->r_state = RT_ADD;
372 _dl_debug_addr->r_state = RT_CONSISTENT;
377 #if defined (__LIBDL_SHARED__)
378 /* Run the ctors and setup the dtors */
379 for (i = nlist; i; --i) {
380 tpnt = init_fini_list[i-1];
381 if (tpnt->init_flag & INIT_FUNCS_CALLED)
383 tpnt->init_flag |= INIT_FUNCS_CALLED;
385 if (tpnt->dynamic_info[DT_INIT]) {
386 void (*dl_elf_func) (void);
387 dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
388 if (dl_elf_func && *dl_elf_func != NULL) {
389 #ifdef __SUPPORT_LD_DEBUG__
391 fprintf(stderr, "running ctors for library %s at '%x'\n", tpnt->libname, (unsigned)dl_elf_func);
399 return (void *) dyn_chain;
402 /* Something went wrong. Clean up and return NULL. */
404 do_dlclose(dyn_chain, 0);
408 void *dlsym(void *vhandle, const char *name)
410 struct elf_resolve *tpnt, *tfrom;
411 struct dyn_elf *handle;
413 struct dyn_elf *rpnt;
416 handle = (struct dyn_elf *) vhandle;
418 /* First of all verify that we have a real handle
419 of some kind. Return NULL if not a valid handle. */
422 handle = _dl_symbol_tables;
423 else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
424 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
428 _dl_error_number = LD_BAD_HANDLE;
431 } else if (handle == RTLD_NEXT) {
433 * Try and locate the module we were called from - we
434 * need this so that we know where to start searching
435 * from. We never pass RTLD_NEXT down into the actual
436 * dynamic loader itself, as it doesn't know
437 * how to properly treat it.
439 from = (ElfW(Addr)) __builtin_return_address(0);
442 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
444 if (tpnt->loadaddr < from
445 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
452 ret = _dl_find_hash((char*)name, handle, NULL, 0);
458 _dl_error_number = LD_NO_SYMBOL;
462 static int do_dlclose(void *vhandle, int need_fini)
464 struct dyn_elf *rpnt, *rpnt1;
465 struct init_fini_list *runp, *tmp;
467 struct elf_resolve *tpnt, *run_tpnt;
468 int (*dl_elf_fini) (void);
469 void (*dl_brk) (void);
470 struct dyn_elf *handle;
474 handle = (struct dyn_elf *) vhandle;
475 if (handle == _dl_symbol_tables)
478 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
485 _dl_error_number = LD_BAD_HANDLE;
489 rpnt1->next_handle = rpnt->next_handle;
491 _dl_handles = rpnt->next_handle;
492 #ifdef __SUPPORT_LD_DEBUG__
494 fprintf(stderr, "dlclose: %s, usage count: %d\n", handle->dyn->libname, handle->dyn->usage_count);
496 if (handle->dyn->usage_count != 1) {
497 handle->dyn->usage_count--;
501 /* OK, this is a valid handle - now close out the file */
502 for (j = 0; j < handle->init_fini.nlist; ++j) {
503 tpnt = handle->init_fini.init_fini[j];
504 if (--tpnt->usage_count == 0) {
505 if (tpnt->dynamic_info[DT_FINI] && need_fini &&
506 !(tpnt->init_flag & FINI_FUNCS_CALLED)) {
507 tpnt->init_flag |= FINI_FUNCS_CALLED;
508 dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
509 #ifdef __SUPPORT_LD_DEBUG__
511 fprintf(stderr, "running dtors for library %s at '%x'\n", tpnt->libname, (unsigned)dl_elf_fini);
516 #ifdef __SUPPORT_LD_DEBUG__
518 fprintf(stderr, "dlclose unmapping: %s\n", tpnt->libname);
521 for (i = 0, ppnt = tpnt->ppnt;
522 i < tpnt->n_phent; ppnt++, i++) {
523 if (ppnt->p_type != PT_LOAD)
525 if (end < ppnt->p_vaddr + ppnt->p_memsz)
526 end = ppnt->p_vaddr + ppnt->p_memsz;
528 _dl_munmap((void*)tpnt->loadaddr, end);
529 /* Free elements in RTLD_LOCAL scope list */
530 for (runp = tpnt->rtld_local; runp; runp = tmp) {
535 /* Next, remove tpnt from the loaded_module list */
536 if (_dl_loaded_modules == tpnt) {
537 _dl_loaded_modules = tpnt->next;
538 if (_dl_loaded_modules)
539 _dl_loaded_modules->prev = 0;
541 for (run_tpnt = _dl_loaded_modules; run_tpnt; run_tpnt = run_tpnt->next)
542 if (run_tpnt->next == tpnt) {
543 #ifdef __SUPPORT_LD_DEBUG__
545 fprintf(stderr, "dlclose removing loaded_modules: %s\n", tpnt->libname);
547 run_tpnt->next = run_tpnt->next->next;
549 run_tpnt->next->prev = run_tpnt;
553 /* Next, remove tpnt from the global symbol table list */
554 if (_dl_symbol_tables->dyn == tpnt) {
555 _dl_symbol_tables = _dl_symbol_tables->next;
556 if (_dl_symbol_tables)
557 _dl_symbol_tables->prev = 0;
559 for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
560 if (rpnt1->next->dyn == tpnt) {
561 #ifdef __SUPPORT_LD_DEBUG__
563 fprintf(stderr, "dlclose removing symbol_tables: %s\n", tpnt->libname);
566 rpnt1->next = rpnt1->next->next;
568 rpnt1->next->prev = rpnt1;
576 free(handle->init_fini.init_fini);
580 if (_dl_debug_addr) {
581 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
582 if (dl_brk != NULL) {
583 _dl_debug_addr->r_state = RT_DELETE;
586 _dl_debug_addr->r_state = RT_CONSISTENT;
594 int dlclose(void *vhandle)
596 return do_dlclose(vhandle, 1);
603 if (!_dl_error_number)
605 retval = dl_error_names[_dl_error_number];
606 _dl_error_number = 0;
607 return (char *)retval;
611 * Dump information to stderrr about the current loaded modules
613 static char *type[] = { "Lib", "Exe", "Int", "Mod" };
617 struct elf_resolve *tpnt;
618 struct dyn_elf *rpnt, *hpnt;
620 fprintf(stderr, "List of loaded modules\n");
621 /* First start with a complete list of all of the loaded files. */
622 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
623 fprintf(stderr, "\t%x %x %x %s %d %s\n",
624 (unsigned) tpnt->loadaddr, (unsigned) tpnt,
625 (unsigned) tpnt->symbol_scope,
627 tpnt->usage_count, tpnt->libname);
630 /* Next dump the module list for the application itself */
631 fprintf(stderr, "\nModules for application (%x):\n",
632 (unsigned) _dl_symbol_tables);
633 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
634 fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname);
636 for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
637 fprintf(stderr, "Modules for handle %x\n", (unsigned) hpnt);
638 for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
639 fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn,
645 int dladdr(const void *__address, Dl_info * __info)
647 struct elf_resolve *pelf;
648 struct elf_resolve *rpnt;
653 * Try and locate the module address is in
658 fprintf(stderr, "dladdr( %x, %x )\n", __address, __info);
661 for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
662 struct elf_resolve *tpnt;
666 fprintf(stderr, "Module \"%s\" at %x\n",
667 tpnt->libname, tpnt->loadaddr);
669 if (tpnt->loadaddr < (ElfW(Addr)) __address
670 && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
680 * Try and locate the symbol of address
692 symtab = (Elf32_Sym *) (pelf->dynamic_info[DT_SYMTAB]);
693 strtab = (char *) (pelf->dynamic_info[DT_STRTAB]);
696 for (hn = 0; hn < pelf->nbucket; hn++) {
697 for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
698 ElfW(Addr) symbol_addr;
700 symbol_addr = pelf->loadaddr + symtab[si].st_value;
701 if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
707 fprintf(stderr, "Symbol \"%s\" at %x\n",
708 strtab + symtab[si].st_name, symbol_addr);
714 __info->dli_fname = pelf->libname;
715 __info->dli_fbase = (void *)pelf->loadaddr;
716 __info->dli_sname = strtab + symtab[sn].st_name;
717 __info->dli_saddr = (void *)sa;