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
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 extern struct elf_resolve * _dl_load_shared_library(int, struct dyn_elf **,
45 struct elf_resolve *, char *, int);
46 extern int _dl_fixup(struct dyn_elf *rpnt, int lazy);
47 extern void _dl_protect_relro(struct elf_resolve * tpnt);
49 extern struct dyn_elf *_dl_symbol_tables;
50 extern struct dyn_elf *_dl_handles;
51 extern struct elf_resolve *_dl_loaded_modules;
52 extern struct r_debug *_dl_debug_addr;
53 extern unsigned long _dl_error_number;
54 extern void *(*_dl_malloc_function)(size_t);
55 #ifdef __LDSO_CACHE_SUPPORT__
56 int _dl_map_cache(void);
57 int _dl_unmap_cache(void);
60 extern void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt, int lazy);
62 #ifdef __SUPPORT_LD_DEBUG__
63 extern char *_dl_debug;
69 /* When libdl is linked as a static library, we need to replace all
70 * the symbols that otherwise would have been loaded in from ldso... */
72 #ifdef __SUPPORT_LD_DEBUG__
75 const char *_dl_progname = ""; /* Program name */
76 char *_dl_library_path = 0; /* Where we look for libraries */
77 char *_dl_ldsopath = 0; /* Location of the shared lib loader */
78 int _dl_errno = 0; /* We can't use the real errno in ldso */
79 size_t _dl_pagesize = PAGE_SIZE; /* Store the page size for use later */
80 /* This global variable is also to communicate with debuggers such as gdb. */
81 struct r_debug *_dl_debug_addr = NULL;
82 #define _dl_malloc malloc
83 #include "../ldso/dl-debug.c"
84 #include "dl-progname.h"
85 #include "../ldso/dl-hash.c"
86 #define _dl_trace_loaded_objects 0
87 #include "../ldso/dl-elf.c"
90 #ifdef __SUPPORT_LD_DEBUG__
91 # define _dl_if_debug_print(fmt, args...) \
94 fprintf(stderr, "%s():%i: " fmt, __FUNCTION__, __LINE__, ## args); \
97 # define _dl_if_debug_print(fmt, args...)
100 static int do_dlclose(void *, int need_fini);
103 static const char *dl_error_names[] = {
106 "Unable to open /dev/zero",
108 #if defined (__i386__)
110 #elif defined (__sparc__)
112 #elif defined (__mc68000__)
115 "Unrecognized binary type",
117 "Not an ELF shared library",
118 "Unable to mmap file",
119 "No dynamic section",
120 #ifdef ELF_USES_RELOCA
121 "Unable to process REL relocs",
123 "Unable to process RELA relocs",
126 "Unable to resolve symbol"
129 void __attribute__ ((destructor)) dl_cleanup(void)
132 for (d = _dl_handles; d; d = d->next_handle) {
137 void *dlopen(const char *libname, int flag)
139 struct elf_resolve *tpnt, *tfrom;
140 struct dyn_elf *dyn_chain, *rpnt = NULL, *dyn_ptr, *relro_ptr, *handle;
141 struct dyn_elf *dpnt;
143 struct elf_resolve *tpnt1;
144 void (*dl_brk) (void);
146 struct init_fini_list *tmp, *runp, *runp2, *dep_list;
148 struct elf_resolve **init_fini_list;
150 /* A bit of sanity checking... */
151 if (!(flag & (RTLD_LAZY|RTLD_NOW))) {
152 _dl_error_number = LD_BAD_HANDLE;
156 from = (ElfW(Addr)) __builtin_return_address(0);
158 /* Cover the trivial case first */
160 return _dl_symbol_tables;
165 * Try and locate the module we were called from - we
166 * need this so that we get the correct RPATH/RUNPATH. Note that
167 * this is the current behavior under Solaris, but the
168 * ABI+ specifies that we should only use the RPATH from
169 * the application. Thus this may go away at some time
173 for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
175 if (tpnt->loadaddr < from
176 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr))
179 for(rpnt = _dl_symbol_tables; rpnt && rpnt->next; rpnt=rpnt->next);
182 now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
183 if (getenv("LD_BIND_NOW"))
186 /* Try to load the specified library */
187 _dl_if_debug_print("Trying to dlopen '%s', RTLD_GLOBAL:%d RTLD_NOW:%d\n",
188 (char*)libname, (flag & RTLD_GLOBAL ? 1:0), (now_flag & RTLD_NOW ? 1:0));
189 tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname, 0);
195 dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
196 _dl_memset(dyn_chain, 0, sizeof(struct dyn_elf));
197 dyn_chain->dyn = tpnt;
198 tpnt->rtld_flags |= (flag & RTLD_GLOBAL);
200 dyn_chain->next_handle = _dl_handles;
201 _dl_handles = dyn_ptr = dyn_chain;
203 if (tpnt->usage_count > 1) {
204 _dl_if_debug_print("Lib: %s already opened\n", libname);
205 /* see if there is a handle from a earlier dlopen */
206 for (handle = _dl_handles->next_handle; handle; handle = handle->next_handle) {
207 if (handle->dyn == tpnt) {
208 dyn_chain->init_fini.init_fini = handle->init_fini.init_fini;
209 dyn_chain->init_fini.nlist = handle->init_fini.nlist;
210 for(i=0; i < dyn_chain->init_fini.nlist; i++)
211 dyn_chain->init_fini.init_fini[i]->rtld_flags |= (flag & RTLD_GLOBAL);
212 dyn_chain->next = handle->next;
218 tpnt->init_flag |= DL_OPENED;
221 _dl_if_debug_print("Looking for needed libraries\n");
223 runp = alloca(sizeof(*runp));
226 dep_list = runp2 = runp;
227 for (; runp; runp = runp->next)
233 runp->tpnt->init_fini = NULL; /* clear any previous dependcies */
234 for (dpnt = (ElfW(Dyn) *) runp->tpnt->dynamic_addr; dpnt->d_tag; dpnt++) {
235 if (dpnt->d_tag == DT_NEEDED) {
238 lpntstr = (char*) (runp->tpnt->dynamic_info[DT_STRTAB] +
240 name = _dl_get_last_path_component(lpntstr);
241 _dl_if_debug_print("Trying to load '%s', needed by '%s'\n",
242 lpntstr, runp->tpnt->libname);
243 tpnt1 = _dl_load_shared_library(0, &rpnt, runp->tpnt, lpntstr, 0);
247 tpnt1->rtld_flags |= (flag & RTLD_GLOBAL);
249 if (tpnt1->usage_count == 1) {
250 tpnt1->init_flag |= DL_OPENED;
251 /* This list is for dlsym() and relocation */
252 dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
253 _dl_memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
254 dyn_ptr = dyn_ptr->next;
255 dyn_ptr->dyn = tpnt1;
257 if (tpnt1->init_flag & DL_OPENED) {
258 /* Used to record RTLD_LOCAL scope */
259 tmp = alloca(sizeof(struct init_fini_list));
261 tmp->next = runp->tpnt->init_fini;
262 runp->tpnt->init_fini = tmp;
264 for (tmp=dep_list; tmp; tmp = tmp->next) {
265 if (tpnt1 == tmp->tpnt) { /* if match => cirular dependency, drop it */
266 _dl_if_debug_print("Circular dependency, skipping '%s',\n",
268 tpnt1->usage_count--;
272 if (!tmp) { /* Don't add if circular dependency detected */
273 runp2->next = alloca(sizeof(*runp));
282 init_fini_list = malloc(nlist * sizeof(struct elf_resolve *));
283 dyn_chain->init_fini.init_fini = init_fini_list;
284 dyn_chain->init_fini.nlist = nlist;
286 for (runp2 = dep_list; runp2; runp2 = runp2->next) {
287 init_fini_list[i++] = runp2->tpnt;
288 for(runp = runp2->tpnt->init_fini; runp; runp = runp->next){
289 if (!(runp->tpnt->rtld_flags & RTLD_GLOBAL)) {
290 tmp = malloc(sizeof(struct init_fini_list));
291 tmp->tpnt = runp->tpnt;
292 tmp->next = runp2->tpnt->rtld_local;
293 runp2->tpnt->rtld_local = tmp;
298 /* Sort the INIT/FINI list in dependency order. */
299 for (runp2 = dep_list; runp2; runp2 = runp2->next) {
301 for (j = 0; init_fini_list[j] != runp2->tpnt; ++j)
303 for (k = j + 1; k < nlist; ++k) {
304 struct init_fini_list *runp = init_fini_list[k]->init_fini;
306 for (; runp; runp = runp->next) {
307 if (runp->tpnt == runp2->tpnt) {
308 struct elf_resolve *here = init_fini_list[k];
309 _dl_if_debug_print("Move %s from pos %d to %d in INIT/FINI list.\n", here->libname, k, j);
310 for (i = (k - j); i; --i)
311 init_fini_list[i+j] = init_fini_list[i+j-1];
312 init_fini_list[j] = here;
319 #ifdef __SUPPORT_LD_DEBUG__
321 fprintf(stderr, "\nINIT/FINI order and dependencies:\n");
322 for (i=0;i < nlist;i++) {
323 fprintf(stderr, "lib: %s has deps:\n", init_fini_list[i]->libname);
324 runp = init_fini_list[i]->init_fini;
325 for (; runp; runp = runp->next)
326 printf(" %s ", runp->tpnt->libname);
332 _dl_if_debug_print("Beginning dlopen relocation fixups\n");
334 * OK, now all of the kids are tucked into bed in their proper addresses.
335 * Now we go through and look for REL and RELA records that indicate fixups
336 * to the GOT tables. We need to do this in reverse order so that COPY
337 * directives work correctly */
340 * Relocation of the GOT entries for MIPS have to be done
341 * after all the libraries have been loaded.
343 _dl_perform_mips_global_got_relocations(tpnt, !now_flag);
346 if (_dl_fixup(dyn_chain, now_flag))
349 /* Notify the debugger we have added some objects. */
350 if (_dl_debug_addr) {
351 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
352 if (dl_brk != NULL) {
353 _dl_debug_addr->r_state = RT_ADD;
356 _dl_debug_addr->r_state = RT_CONSISTENT;
362 /* Run the ctors and setup the dtors */
363 for (i = nlist; i; --i) {
364 tpnt = init_fini_list[i-1];
365 if (tpnt->init_flag & INIT_FUNCS_CALLED)
367 tpnt->init_flag |= INIT_FUNCS_CALLED;
369 if (tpnt->dynamic_info[DT_INIT]) {
370 void (*dl_elf_func) (void);
371 dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
372 if (dl_elf_func && *dl_elf_func != NULL) {
373 _dl_if_debug_print("running ctors for library %s at '%p'\n",
374 tpnt->libname, dl_elf_func);
382 for (rpnt = relro_ptr->next; rpnt; rpnt = rpnt->next) {
383 if (rpnt->dyn->relro_size)
384 _dl_protect_relro(rpnt->dyn);
387 /* TODO: Should we set the protections of all pages back to R/O now ? */
390 return (void *) dyn_chain;
393 /* Something went wrong. Clean up and return NULL. */
395 do_dlclose(dyn_chain, 0);
399 void *dlsym(void *vhandle, const char *name)
401 struct elf_resolve *tpnt, *tfrom;
402 struct dyn_elf *handle;
404 struct dyn_elf *rpnt;
407 handle = (struct dyn_elf *) vhandle;
409 /* First of all verify that we have a real handle
410 of some kind. Return NULL if not a valid handle. */
413 handle = _dl_symbol_tables;
414 else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
415 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
419 _dl_error_number = LD_BAD_HANDLE;
422 } else if (handle == RTLD_NEXT) {
424 * Try and locate the module we were called from - we
425 * need this so that we know where to start searching
426 * from. We never pass RTLD_NEXT down into the actual
427 * dynamic loader itself, as it doesn't know
428 * how to properly treat it.
430 from = (ElfW(Addr)) __builtin_return_address(0);
433 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
435 if (tpnt->loadaddr < from
436 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
443 ret = _dl_find_hash((char*)name, handle, NULL, 0);
449 _dl_error_number = LD_NO_SYMBOL;
454 void *dlvsym(void *vhandle, const char *name, const char *version)
456 return dlsym(vhandle, name);
460 static int do_dlclose(void *vhandle, int need_fini)
462 struct dyn_elf *rpnt, *rpnt1, *rpnt1_tmp;
463 struct init_fini_list *runp, *tmp;
465 struct elf_resolve *tpnt, *run_tpnt;
466 int (*dl_elf_fini) (void);
467 void (*dl_brk) (void);
468 struct dyn_elf *handle;
472 handle = (struct dyn_elf *) vhandle;
473 if (handle == _dl_symbol_tables)
476 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
483 _dl_error_number = LD_BAD_HANDLE;
487 rpnt1->next_handle = rpnt->next_handle;
489 _dl_handles = rpnt->next_handle;
490 _dl_if_debug_print("%s: usage count: %d\n",
491 handle->dyn->libname, handle->dyn->usage_count);
492 if (handle->dyn->usage_count != 1) {
493 handle->dyn->usage_count--;
497 /* OK, this is a valid handle - now close out the file */
498 for (j = 0; j < handle->init_fini.nlist; ++j) {
499 tpnt = handle->init_fini.init_fini[j];
500 if (--tpnt->usage_count == 0) {
501 if (tpnt->dynamic_info[DT_FINI] && need_fini &&
502 !(tpnt->init_flag & FINI_FUNCS_CALLED)) {
503 tpnt->init_flag |= FINI_FUNCS_CALLED;
504 dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
505 _dl_if_debug_print("running dtors for library %s at '%p'\n",
506 tpnt->libname, dl_elf_fini);
510 _dl_if_debug_print("unmapping: %s\n", tpnt->libname);
512 for (i = 0, ppnt = tpnt->ppnt;
513 i < tpnt->n_phent; ppnt++, i++) {
514 if (ppnt->p_type != PT_LOAD)
516 if (end < ppnt->p_vaddr + ppnt->p_memsz)
517 end = ppnt->p_vaddr + ppnt->p_memsz;
519 _dl_munmap((void*)tpnt->loadaddr, end);
520 /* Free elements in RTLD_LOCAL scope list */
521 for (runp = tpnt->rtld_local; runp; runp = tmp) {
526 /* Next, remove tpnt from the loaded_module list */
527 if (_dl_loaded_modules == tpnt) {
528 _dl_loaded_modules = tpnt->next;
529 if (_dl_loaded_modules)
530 _dl_loaded_modules->prev = 0;
532 for (run_tpnt = _dl_loaded_modules; run_tpnt; run_tpnt = run_tpnt->next)
533 if (run_tpnt->next == tpnt) {
534 _dl_if_debug_print("removing loaded_modules: %s\n", tpnt->libname);
535 run_tpnt->next = run_tpnt->next->next;
537 run_tpnt->next->prev = run_tpnt;
541 /* Next, remove tpnt from the global symbol table list */
542 if (_dl_symbol_tables) {
543 if (_dl_symbol_tables->dyn == tpnt) {
544 _dl_symbol_tables = _dl_symbol_tables->next;
545 if (_dl_symbol_tables)
546 _dl_symbol_tables->prev = 0;
548 for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
549 if (rpnt1->next->dyn == tpnt) {
550 _dl_if_debug_print("removing symbol_tables: %s\n", tpnt->libname);
551 rpnt1_tmp = rpnt1->next->next;
553 rpnt1->next = rpnt1_tmp;
555 rpnt1->next->prev = rpnt1;
564 free(handle->init_fini.init_fini);
568 if (_dl_debug_addr) {
569 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
570 if (dl_brk != NULL) {
571 _dl_debug_addr->r_state = RT_DELETE;
574 _dl_debug_addr->r_state = RT_CONSISTENT;
582 int dlclose(void *vhandle)
584 return do_dlclose(vhandle, 1);
591 if (!_dl_error_number)
593 retval = dl_error_names[_dl_error_number];
594 _dl_error_number = 0;
595 return (char *)retval;
599 * Dump information to stderrr about the current loaded modules
601 static char *type[] = { "Lib", "Exe", "Int", "Mod" };
605 struct elf_resolve *tpnt;
606 struct dyn_elf *rpnt, *hpnt;
608 fprintf(stderr, "List of loaded modules\n");
609 /* First start with a complete list of all of the loaded files. */
610 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
611 fprintf(stderr, "\t%p %p %p %s %d %s\n",
612 tpnt->loadaddr, tpnt, tpnt->symbol_scope,
614 tpnt->usage_count, tpnt->libname);
617 /* Next dump the module list for the application itself */
618 fprintf(stderr, "\nModules for application (%p):\n", _dl_symbol_tables);
619 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
620 fprintf(stderr, "\t%p %s\n", rpnt->dyn, rpnt->dyn->libname);
622 for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
623 fprintf(stderr, "Modules for handle %p\n", hpnt);
624 for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
625 fprintf(stderr, "\t%p %s\n", rpnt->dyn, rpnt->dyn->libname);
630 int dladdr(const void *__address, Dl_info * __info)
632 struct elf_resolve *pelf;
633 struct elf_resolve *rpnt;
638 * Try and locate the module address is in
643 fprintf(stderr, "dladdr( %p, %p )\n", __address, __info);
646 for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
647 struct elf_resolve *tpnt;
651 fprintf(stderr, "Module \"%s\" at %p\n",
652 tpnt->libname, tpnt->loadaddr);
654 if (tpnt->loadaddr < (ElfW(Addr)) __address
655 && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
665 * Try and locate the symbol of address
677 symtab = (ElfW(Sym) *) (pelf->dynamic_info[DT_SYMTAB]);
678 strtab = (char *) (pelf->dynamic_info[DT_STRTAB]);
681 for (hn = 0; hn < pelf->nbucket; hn++) {
682 for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
683 ElfW(Addr) symbol_addr;
685 symbol_addr = pelf->loadaddr + symtab[si].st_value;
686 if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
692 fprintf(stderr, "Symbol \"%s\" at %p\n",
693 strtab + symtab[si].st_name, symbol_addr);
699 __info->dli_fname = pelf->libname;
700 __info->dli_fbase = (void *)pelf->loadaddr;
701 __info->dli_sname = strtab + symtab[sn].st_name;
702 __info->dli_saddr = (void *)sa;