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
39 /* When libdl is loaded as a shared library, we need to load in
40 * and use a pile of symbols from ldso... */
42 extern char *_dl_find_hash(const char *, struct dyn_elf *, struct elf_resolve *, int);
43 extern struct elf_resolve * _dl_load_shared_library(int, struct dyn_elf **,
44 struct elf_resolve *, char *, int);
45 extern int _dl_fixup(struct dyn_elf *rpnt, int lazy);
46 extern void _dl_protect_relro(struct elf_resolve * tpnt);
48 extern struct dyn_elf *_dl_symbol_tables;
49 extern struct dyn_elf *_dl_handles;
50 extern struct elf_resolve *_dl_loaded_modules;
51 extern struct r_debug *_dl_debug_addr;
52 extern unsigned long _dl_error_number;
53 extern void *(*_dl_malloc_function)(size_t);
54 extern void _dl_run_init_array(struct elf_resolve *);
55 extern void _dl_run_fini_array(struct elf_resolve *);
56 #ifdef __LDSO_CACHE_SUPPORT__
57 int _dl_map_cache(void);
58 int _dl_unmap_cache(void);
61 extern void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt, int lazy);
63 #ifdef __SUPPORT_LD_DEBUG__
64 extern char *_dl_debug;
70 /* When libdl is linked as a static library, we need to replace all
71 * the symbols that otherwise would have been loaded in from ldso... */
73 #ifdef __SUPPORT_LD_DEBUG__
76 const char *_dl_progname = ""; /* Program name */
77 char *_dl_library_path = 0; /* Where we look for libraries */
78 char *_dl_ldsopath = 0; /* Location of the shared lib loader */
79 int _dl_errno = 0; /* We can't use the real errno in ldso */
80 size_t _dl_pagesize = PAGE_SIZE; /* Store the page size for use later */
81 /* This global variable is also to communicate with debuggers such as gdb. */
82 struct r_debug *_dl_debug_addr = NULL;
83 #define _dl_malloc malloc
84 #include "../ldso/dl-debug.c"
85 #include LDSO_ELFINTERP
86 #include "../ldso/dl-hash.c"
87 #define _dl_trace_loaded_objects 0
88 #include "../ldso/dl-elf.c"
91 #ifdef __SUPPORT_LD_DEBUG__
92 # define _dl_if_debug_print(fmt, args...) \
95 fprintf(stderr, "%s():%i: " fmt, __FUNCTION__, __LINE__, ## args); \
98 # define _dl_if_debug_print(fmt, args...)
101 static int do_dlclose(void *, int need_fini);
104 static const char *dl_error_names[] = {
107 "Unable to open /dev/zero",
109 #if defined (__i386__)
111 #elif defined (__sparc__)
113 #elif defined (__mc68000__)
116 "Unrecognized binary type",
118 "Not an ELF shared library",
119 "Unable to mmap file",
120 "No dynamic section",
121 #ifdef ELF_USES_RELOCA
122 "Unable to process REL relocs",
124 "Unable to process RELA relocs",
127 "Unable to resolve symbol"
130 void dl_cleanup(void) __attribute__ ((destructor));
131 void dl_cleanup(void)
134 for (d = _dl_handles; d; d = d->next_handle) {
139 void *dlopen(const char *libname, int flag)
141 struct elf_resolve *tpnt, *tfrom;
142 struct dyn_elf *dyn_chain, *rpnt = NULL, *dyn_ptr, *relro_ptr, *handle;
144 struct elf_resolve *tpnt1;
145 void (*dl_brk) (void);
147 struct init_fini_list *tmp, *runp, *runp2, *dep_list;
148 unsigned int nlist, i;
149 struct elf_resolve **init_fini_list;
151 /* A bit of sanity checking... */
152 if (!(flag & (RTLD_LAZY|RTLD_NOW))) {
153 _dl_error_number = LD_BAD_HANDLE;
157 from = (ElfW(Addr)) __builtin_return_address(0);
159 /* Cover the trivial case first */
161 return _dl_symbol_tables;
166 * Try and locate the module we were called from - we
167 * need this so that we get the correct RPATH/RUNPATH. Note that
168 * this is the current behavior under Solaris, but the
169 * ABI+ specifies that we should only use the RPATH from
170 * the application. Thus this may go away at some time
174 struct dyn_elf *dpnt;
176 for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
178 if (tpnt->loadaddr < from
179 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr))
183 for(rpnt = _dl_symbol_tables; rpnt && rpnt->next; rpnt=rpnt->next);
186 now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
187 if (getenv("LD_BIND_NOW"))
190 /* Try to load the specified library */
191 _dl_if_debug_print("Trying to dlopen '%s', RTLD_GLOBAL:%d RTLD_NOW:%d\n",
192 (char*)libname, (flag & RTLD_GLOBAL ? 1:0), (now_flag & RTLD_NOW ? 1:0));
193 tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname, 0);
199 dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
200 _dl_memset(dyn_chain, 0, sizeof(struct dyn_elf));
201 dyn_chain->dyn = tpnt;
202 tpnt->rtld_flags |= (flag & RTLD_GLOBAL);
204 dyn_chain->next_handle = _dl_handles;
205 _dl_handles = dyn_ptr = dyn_chain;
207 if (tpnt->usage_count > 1) {
208 _dl_if_debug_print("Lib: %s already opened\n", libname);
209 /* see if there is a handle from a earlier dlopen */
210 for (handle = _dl_handles->next_handle; handle; handle = handle->next_handle) {
211 if (handle->dyn == tpnt) {
212 dyn_chain->init_fini.init_fini = handle->init_fini.init_fini;
213 dyn_chain->init_fini.nlist = handle->init_fini.nlist;
214 for(i=0; i < dyn_chain->init_fini.nlist; i++)
215 dyn_chain->init_fini.init_fini[i]->rtld_flags |= (flag & RTLD_GLOBAL);
216 dyn_chain->next = handle->next;
222 tpnt->init_flag |= DL_OPENED;
225 _dl_if_debug_print("Looking for needed libraries\n");
227 runp = alloca(sizeof(*runp));
230 dep_list = runp2 = runp;
231 for (; runp; runp = runp->next)
237 runp->tpnt->init_fini = NULL; /* clear any previous dependcies */
238 for (dpnt = (ElfW(Dyn) *) runp->tpnt->dynamic_addr; dpnt->d_tag; dpnt++) {
239 if (dpnt->d_tag == DT_NEEDED) {
240 lpntstr = (char*) (runp->tpnt->dynamic_info[DT_STRTAB] +
242 _dl_if_debug_print("Trying to load '%s', needed by '%s'\n",
243 lpntstr, runp->tpnt->libname);
244 tpnt1 = _dl_load_shared_library(0, &rpnt, runp->tpnt, lpntstr, 0);
248 tpnt1->rtld_flags |= (flag & RTLD_GLOBAL);
250 if (tpnt1->usage_count == 1) {
251 tpnt1->init_flag |= DL_OPENED;
252 /* This list is for dlsym() and relocation */
253 dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
254 _dl_memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
255 dyn_ptr = dyn_ptr->next;
256 dyn_ptr->dyn = tpnt1;
258 if (tpnt1->init_flag & DL_OPENED) {
259 /* Used to record RTLD_LOCAL scope */
260 tmp = alloca(sizeof(struct init_fini_list));
262 tmp->next = runp->tpnt->init_fini;
263 runp->tpnt->init_fini = tmp;
265 for (tmp=dep_list; tmp; tmp = tmp->next) {
266 if (tpnt1 == tmp->tpnt) { /* if match => cirular dependency, drop it */
267 _dl_if_debug_print("Circular dependency, skipping '%s',\n",
269 tpnt1->usage_count--;
273 if (!tmp) { /* Don't add if circular dependency detected */
274 runp2->next = alloca(sizeof(*runp));
283 init_fini_list = malloc(nlist * sizeof(struct elf_resolve *));
284 dyn_chain->init_fini.init_fini = init_fini_list;
285 dyn_chain->init_fini.nlist = nlist;
287 for (runp2 = dep_list; runp2; runp2 = runp2->next) {
288 init_fini_list[i++] = runp2->tpnt;
289 for(runp = runp2->tpnt->init_fini; runp; runp = runp->next){
290 if (!(runp->tpnt->rtld_flags & RTLD_GLOBAL)) {
291 tmp = malloc(sizeof(struct init_fini_list));
292 tmp->tpnt = runp->tpnt;
293 tmp->next = runp2->tpnt->rtld_local;
294 runp2->tpnt->rtld_local = tmp;
299 /* Sort the INIT/FINI list in dependency order. */
300 for (runp2 = dep_list; runp2; runp2 = runp2->next) {
302 for (j = 0; init_fini_list[j] != runp2->tpnt; ++j)
304 for (k = j + 1; k < nlist; ++k) {
305 struct init_fini_list *ele = init_fini_list[k]->init_fini;
307 for (; ele; ele = ele->next) {
308 if (ele->tpnt == runp2->tpnt) {
309 struct elf_resolve *here = init_fini_list[k];
310 _dl_if_debug_print("Move %s from pos %d to %d in INIT/FINI list.\n", here->libname, k, j);
311 for (i = (k - j); i; --i)
312 init_fini_list[i+j] = init_fini_list[i+j-1];
313 init_fini_list[j] = here;
320 #ifdef __SUPPORT_LD_DEBUG__
322 fprintf(stderr, "\nINIT/FINI order and dependencies:\n");
323 for (i=0;i < nlist;i++) {
324 fprintf(stderr, "lib: %s has deps:\n", init_fini_list[i]->libname);
325 runp = init_fini_list[i]->init_fini;
326 for (; runp; runp = runp->next)
327 printf(" %s ", runp->tpnt->libname);
333 _dl_if_debug_print("Beginning dlopen relocation fixups\n");
335 * OK, now all of the kids are tucked into bed in their proper addresses.
336 * Now we go through and look for REL and RELA records that indicate fixups
337 * to the GOT tables. We need to do this in reverse order so that COPY
338 * directives work correctly */
341 * Relocation of the GOT entries for MIPS have to be done
342 * after all the libraries have been loaded.
344 _dl_perform_mips_global_got_relocations(tpnt, !now_flag);
347 if (_dl_fixup(dyn_chain, now_flag))
351 for (rpnt = relro_ptr->next; rpnt; rpnt = rpnt->next) {
352 if (rpnt->dyn->relro_size)
353 _dl_protect_relro(rpnt->dyn);
356 /* TODO: Should we set the protections of all pages back to R/O now ? */
359 /* Notify the debugger we have added some objects. */
360 if (_dl_debug_addr) {
361 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
362 if (dl_brk != NULL) {
363 _dl_debug_addr->r_state = RT_ADD;
366 _dl_debug_addr->r_state = RT_CONSISTENT;
372 /* Run the ctors and setup the dtors */
373 for (i = nlist; i; --i) {
374 tpnt = init_fini_list[i-1];
375 if (tpnt->init_flag & INIT_FUNCS_CALLED)
377 tpnt->init_flag |= INIT_FUNCS_CALLED;
379 if (tpnt->dynamic_info[DT_INIT]) {
380 void (*dl_elf_func) (void);
381 dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
382 if (dl_elf_func && *dl_elf_func != NULL) {
383 _dl_if_debug_print("running ctors for library %s at '%p'\n",
384 tpnt->libname, dl_elf_func);
389 _dl_run_init_array(tpnt);
394 return (void *) dyn_chain;
397 /* Something went wrong. Clean up and return NULL. */
399 do_dlclose(dyn_chain, 0);
403 void *dlsym(void *vhandle, const char *name)
405 struct elf_resolve *tpnt, *tfrom;
406 struct dyn_elf *handle;
408 struct dyn_elf *rpnt;
411 handle = (struct dyn_elf *) vhandle;
413 /* First of all verify that we have a real handle
414 of some kind. Return NULL if not a valid handle. */
417 handle = _dl_symbol_tables;
418 else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
419 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
423 _dl_error_number = LD_BAD_HANDLE;
426 } else if (handle == RTLD_NEXT) {
428 * Try and locate the module we were called from - we
429 * need this so that we know where to start searching
430 * from. We never pass RTLD_NEXT down into the actual
431 * dynamic loader itself, as it doesn't know
432 * how to properly treat it.
434 from = (ElfW(Addr)) __builtin_return_address(0);
437 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
439 if (tpnt->loadaddr < from
440 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
447 ret = _dl_find_hash((char*)name, handle, NULL, 0);
453 _dl_error_number = LD_NO_SYMBOL;
458 void *dlvsym(void *vhandle, const char *name, const char *version)
460 return dlsym(vhandle, name);
464 static int do_dlclose(void *vhandle, int need_fini)
466 struct dyn_elf *rpnt, *rpnt1, *rpnt1_tmp;
467 struct init_fini_list *runp, *tmp;
469 struct elf_resolve *tpnt, *run_tpnt;
470 int (*dl_elf_fini) (void);
471 void (*dl_brk) (void);
472 struct dyn_elf *handle;
476 handle = (struct dyn_elf *) vhandle;
477 if (handle == _dl_symbol_tables)
480 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
487 _dl_error_number = LD_BAD_HANDLE;
491 rpnt1->next_handle = rpnt->next_handle;
493 _dl_handles = rpnt->next_handle;
494 _dl_if_debug_print("%s: usage count: %d\n",
495 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]
506 || tpnt->dynamic_info[DT_FINI_ARRAY])
508 !(tpnt->init_flag & FINI_FUNCS_CALLED)) {
509 tpnt->init_flag |= FINI_FUNCS_CALLED;
511 _dl_run_fini_array(tpnt);
514 if (tpnt->dynamic_info[DT_FINI]) {
515 dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
516 _dl_if_debug_print("running dtors for library %s at '%p'\n",
517 tpnt->libname, dl_elf_fini);
522 _dl_if_debug_print("unmapping: %s\n", tpnt->libname);
524 for (i = 0, ppnt = tpnt->ppnt;
525 i < tpnt->n_phent; ppnt++, i++) {
526 if (ppnt->p_type != PT_LOAD)
528 if (end < ppnt->p_vaddr + ppnt->p_memsz)
529 end = ppnt->p_vaddr + ppnt->p_memsz;
531 _dl_munmap((void*)tpnt->loadaddr, end);
532 /* Free elements in RTLD_LOCAL scope list */
533 for (runp = tpnt->rtld_local; runp; runp = tmp) {
538 /* Next, remove tpnt from the loaded_module list */
539 if (_dl_loaded_modules == tpnt) {
540 _dl_loaded_modules = tpnt->next;
541 if (_dl_loaded_modules)
542 _dl_loaded_modules->prev = 0;
544 for (run_tpnt = _dl_loaded_modules; run_tpnt; run_tpnt = run_tpnt->next)
545 if (run_tpnt->next == tpnt) {
546 _dl_if_debug_print("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) {
555 if (_dl_symbol_tables->dyn == tpnt) {
556 _dl_symbol_tables = _dl_symbol_tables->next;
557 if (_dl_symbol_tables)
558 _dl_symbol_tables->prev = 0;
560 for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
561 if (rpnt1->next->dyn == tpnt) {
562 _dl_if_debug_print("removing symbol_tables: %s\n", tpnt->libname);
563 rpnt1_tmp = rpnt1->next->next;
565 rpnt1->next = rpnt1_tmp;
567 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 stderr about the current loaded modules
614 static char *type[] = { "Lib", "Exe", "Int", "Mod" };
618 struct elf_resolve *tpnt;
619 struct dyn_elf *rpnt, *hpnt;
621 fprintf(stderr, "List of loaded modules\n");
622 /* First start with a complete list of all of the loaded files. */
623 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
624 fprintf(stderr, "\t%p %p %p %s %d %s\n",
625 tpnt->loadaddr, tpnt, 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 (%p):\n", _dl_symbol_tables);
632 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
633 fprintf(stderr, "\t%p %s\n", rpnt->dyn, rpnt->dyn->libname);
635 for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
636 fprintf(stderr, "Modules for handle %p\n", hpnt);
637 for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
638 fprintf(stderr, "\t%p %s\n", rpnt->dyn, rpnt->dyn->libname);
643 int dladdr(const void *__address, Dl_info * __info)
645 struct elf_resolve *pelf;
646 struct elf_resolve *rpnt;
651 * Try and locate the module address is in
656 fprintf(stderr, "dladdr( %p, %p )\n", __address, __info);
659 for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
660 struct elf_resolve *tpnt;
664 fprintf(stderr, "Module \"%s\" at %p\n",
665 tpnt->libname, tpnt->loadaddr);
667 if (tpnt->loadaddr < (ElfW(Addr)) __address
668 && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
678 * Try and locate the symbol of address
684 unsigned int hn, si, sn, sf;
688 symtab = (ElfW(Sym) *) (pelf->dynamic_info[DT_SYMTAB]);
689 strtab = (char *) (pelf->dynamic_info[DT_STRTAB]);
692 for (hn = 0; hn < pelf->nbucket; hn++) {
693 for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
694 ElfW(Addr) symbol_addr;
696 symbol_addr = pelf->loadaddr + symtab[si].st_value;
697 if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
703 fprintf(stderr, "Symbol \"%s\" at %p\n",
704 strtab + symtab[si].st_name, symbol_addr);
710 __info->dli_fname = pelf->libname;
711 __info->dli_fbase = (void *)pelf->loadaddr;
712 __info->dli_sname = strtab + symtab[sn].st_name;
713 __info->dli_saddr = (void *)sa;