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 int _dl_fixup(struct dyn_elf *rpnt, int lazy)
48 __attribute__ ((__weak__));
49 extern void _dl_protect_relro(struct elf_resolve * tpnt)
50 __attribute__ ((__weak__));
51 extern int _dl_errno __attribute__ ((__weak__));
52 extern struct dyn_elf *_dl_symbol_tables __attribute__ ((__weak__));
53 extern struct dyn_elf *_dl_handles __attribute__ ((__weak__));
54 extern struct elf_resolve *_dl_loaded_modules __attribute__ ((__weak__));
55 extern struct r_debug *_dl_debug_addr __attribute__ ((__weak__));
56 extern unsigned long _dl_error_number __attribute__ ((__weak__));
57 extern void *(*_dl_malloc_function)(size_t) __attribute__ ((__weak__));
58 #ifdef __LDSO_CACHE_SUPPORT__
59 int _dl_map_cache(void) __attribute__ ((__weak__));
60 int _dl_unmap_cache(void) __attribute__ ((__weak__));
63 extern void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt, int lazy)
64 __attribute__ ((__weak__));
66 #ifdef __SUPPORT_LD_DEBUG__
67 extern char *_dl_debug __attribute__ ((__weak__));
71 #else /* __LIBDL_SHARED__ */
73 /* When libdl is linked as a static library, we need to replace all
74 * the symbols that otherwise would have been loaded in from ldso... */
76 #ifdef __SUPPORT_LD_DEBUG__
79 char *_dl_library_path = 0; /* Where we look for libraries */
80 char *_dl_ldsopath = 0; /* Location of the shared lib loader */
81 int _dl_errno = 0; /* We can't use the real errno in ldso */
82 size_t _dl_pagesize = PAGE_SIZE; /* Store the page size for use later */
83 /* This global variable is also to communicate with debuggers such as gdb. */
84 struct r_debug *_dl_debug_addr = NULL;
85 #define _dl_malloc malloc
86 #include "dl-progname.h"
87 #include "../ldso/dl-hash.c"
88 #define _dl_trace_loaded_objects 0
89 #include "../ldso/dl-elf.c"
92 static int do_dlclose(void *, int need_fini);
95 static const char *dl_error_names[] = {
98 "Unable to open /dev/zero",
100 #if defined (__i386__)
102 #elif defined (__sparc__)
104 #elif defined (__mc68000__)
107 "Unrecognized binary type",
109 "Not an ELF shared library",
110 "Unable to mmap file",
111 "No dynamic section",
112 #ifdef ELF_USES_RELOCA
113 "Unable to process REL relocs",
115 "Unable to process RELA relocs",
118 "Unable to resolve symbol"
121 void __attribute__ ((destructor)) dl_cleanup(void)
124 for (d = _dl_handles; d; d = d->next_handle) {
129 void *dlopen(const char *libname, int flag)
131 struct elf_resolve *tpnt, *tfrom, *tcurr=NULL;
132 struct dyn_elf *dyn_chain, *rpnt = NULL, *dyn_ptr, *relro_ptr, *handle;
133 struct dyn_elf *dpnt;
135 struct elf_resolve *tpnt1;
136 void (*dl_brk) (void);
138 struct init_fini_list *tmp, *runp, *runp2, *dep_list;
140 struct elf_resolve **init_fini_list;
142 /* A bit of sanity checking... */
143 if (!(flag & (RTLD_LAZY|RTLD_NOW))) {
144 _dl_error_number = LD_BAD_HANDLE;
148 from = (ElfW(Addr)) __builtin_return_address(0);
150 /* Cover the trivial case first */
152 return _dl_symbol_tables;
157 * Try and locate the module we were called from - we
158 * need this so that we get the correct RPATH. Note that
159 * this is the current behavior under Solaris, but the
160 * ABI+ specifies that we should only use the RPATH from
161 * the application. Thus this may go away at some time
165 for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
167 if (tpnt->loadaddr < from
168 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr))
171 for(rpnt = _dl_symbol_tables; rpnt->next; rpnt=rpnt->next);
174 /* Try to load the specified library */
175 #ifdef __SUPPORT_LD_DEBUG__
177 fprintf(stderr, "Trying to dlopen '%s'\n", (char*)libname);
179 tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname, 0);
185 dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
186 _dl_memset(dyn_chain, 0, sizeof(struct dyn_elf));
187 dyn_chain->dyn = tpnt;
188 tpnt->rtld_flags |= (flag & RTLD_GLOBAL);
190 dyn_chain->next_handle = _dl_handles;
191 _dl_handles = dyn_ptr = dyn_chain;
193 if (tpnt->usage_count > 1) {
194 #ifdef __SUPPORT_LD_DEBUG__
196 fprintf(stderr, "Lib: % already opened\n", libname);
198 /* see if there is a handle from a earlier dlopen */
199 for (handle = _dl_handles->next_handle; handle; handle = handle->next_handle) {
200 if (handle->dyn == tpnt) {
201 dyn_chain->init_fini.init_fini = handle->init_fini.init_fini;
202 dyn_chain->init_fini.nlist = handle->init_fini.nlist;
203 for(i=0; i < dyn_chain->init_fini.nlist; i++)
204 dyn_chain->init_fini.init_fini[i]->rtld_flags |= (flag & RTLD_GLOBAL);
205 dyn_chain->next = handle->next;
211 tpnt->init_flag |= DL_OPENED;
214 #ifdef __SUPPORT_LD_DEBUG__
216 fprintf(stderr, "Looking for needed libraries\n");
219 runp = alloca(sizeof(*runp));
222 dep_list = runp2 = runp;
223 for (; runp; runp = runp->next)
229 runp->tpnt->init_fini = NULL; /* clear any previous dependcies */
230 for (dpnt = (Elf32_Dyn *) runp->tpnt->dynamic_addr; dpnt->d_tag; dpnt++) {
231 if (dpnt->d_tag == DT_NEEDED) {
234 lpntstr = (char*) (runp->tpnt->dynamic_info[DT_STRTAB] +
236 name = _dl_get_last_path_component(lpntstr);
237 #ifdef __SUPPORT_LD_DEBUG__
239 fprintf(stderr, "Trying to load '%s', needed by '%s'\n",
240 lpntstr, runp->tpnt->libname);
242 tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpntstr, 0);
246 tpnt1->rtld_flags |= (flag & RTLD_GLOBAL);
248 if (tpnt1->usage_count == 1) {
249 tpnt1->init_flag |= DL_OPENED;
250 /* This list is for dlsym() and relocation */
251 dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
252 _dl_memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
253 dyn_ptr = dyn_ptr->next;
254 dyn_ptr->dyn = tpnt1;
256 if (tpnt1->init_flag & DL_OPENED) {
257 /* Used to record RTLD_LOCAL scope */
258 tmp = alloca(sizeof(struct init_fini_list));
260 tmp->next = runp->tpnt->init_fini;
261 runp->tpnt->init_fini = tmp;
263 for (tmp=dep_list; tmp; tmp = tmp->next) {
264 if (tpnt1 == tmp->tpnt) { /* if match => cirular dependency, drop it */
265 #ifdef __SUPPORT_LD_DEBUG__
267 fprintf(stderr, "Circular dependency, skipping '%s',\n",
270 tpnt1->usage_count--;
274 if (!tmp) { /* Don't add if circular dependency detected */
275 runp2->next = alloca(sizeof(*runp));
284 init_fini_list = malloc(nlist * sizeof(struct elf_resolve *));
285 dyn_chain->init_fini.init_fini = init_fini_list;
286 dyn_chain->init_fini.nlist = nlist;
288 for (runp2 = dep_list; runp2; runp2 = runp2->next) {
289 init_fini_list[i++] = runp2->tpnt;
290 for(runp = runp2->tpnt->init_fini; runp; runp = runp->next){
291 if (!(runp->tpnt->rtld_flags & RTLD_GLOBAL)) {
292 tmp = malloc(sizeof(struct init_fini_list));
293 tmp->tpnt = runp->tpnt;
294 tmp->next = runp2->tpnt->rtld_local;
295 runp2->tpnt->rtld_local = tmp;
300 /* Sort the INIT/FINI list in dependency order. */
301 for (runp2 = dep_list; runp2; runp2 = runp2->next) {
303 for (j = 0; init_fini_list[j] != runp2->tpnt; ++j)
305 for (k = j + 1; k < nlist; ++k) {
306 struct init_fini_list *runp = init_fini_list[k]->init_fini;
308 for (; runp; runp = runp->next) {
309 if (runp->tpnt == runp2->tpnt) {
310 struct elf_resolve *here = init_fini_list[k];
311 #ifdef __SUPPORT_LD_DEBUG__
313 fprintf(stderr, "Move %s from pos %d to %d in INIT/FINI list.\n", here->libname, k, j);
315 for (i = (k - j); i; --i)
316 init_fini_list[i+j] = init_fini_list[i+j-1];
317 init_fini_list[j] = here;
324 #ifdef __SUPPORT_LD_DEBUG__
326 fprintf(stderr, "\nINIT/FINI order and dependencies:\n");
327 for (i=0;i < nlist;i++) {
328 fprintf(stderr, "lib: %s has deps:\n", init_fini_list[i]->libname);
329 runp = init_fini_list[i]->init_fini;
330 for ( ;runp; runp = runp->next)
331 printf(" %s ", runp->tpnt->libname);
337 #ifdef __SUPPORT_LD_DEBUG__
339 fprintf(stderr, "Beginning dlopen relocation fixups\n");
342 * OK, now all of the kids are tucked into bed in their proper addresses.
343 * Now we go through and look for REL and RELA records that indicate fixups
344 * to the GOT tables. We need to do this in reverse order so that COPY
345 * directives work correctly */
346 now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
347 if (getenv("LD_BIND_NOW"))
352 * Relocation of the GOT entries for MIPS have to be done
353 * after all the libraries have been loaded.
355 _dl_perform_mips_global_got_relocations(tpnt, !now_flag);
358 if (_dl_fixup(dyn_chain, now_flag))
361 for (rpnt = relro_ptr->next; rpnt; rpnt = rpnt->next) {
362 if (rpnt->dyn->relro_size)
363 _dl_protect_relro(rpnt->dyn);
365 /* TODO: Should we set the protections of all pages back to R/O now ? */
368 /* Notify the debugger we have added some objects. */
369 if (_dl_debug_addr) {
370 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
371 if (dl_brk != NULL) {
372 _dl_debug_addr->r_state = RT_ADD;
375 _dl_debug_addr->r_state = RT_CONSISTENT;
380 #if defined (__LIBDL_SHARED__)
381 /* Run the ctors and setup the dtors */
382 for (i = nlist; i; --i) {
383 tpnt = init_fini_list[i-1];
384 if (tpnt->init_flag & INIT_FUNCS_CALLED)
386 tpnt->init_flag |= INIT_FUNCS_CALLED;
388 if (tpnt->dynamic_info[DT_INIT]) {
389 void (*dl_elf_func) (void);
390 dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
391 if (dl_elf_func && *dl_elf_func != NULL) {
392 #ifdef __SUPPORT_LD_DEBUG__
394 fprintf(stderr, "running ctors for library %s at '%x'\n", tpnt->libname, (unsigned)dl_elf_func);
402 return (void *) dyn_chain;
405 /* Something went wrong. Clean up and return NULL. */
407 do_dlclose(dyn_chain, 0);
411 void *dlsym(void *vhandle, const char *name)
413 struct elf_resolve *tpnt, *tfrom;
414 struct dyn_elf *handle;
416 struct dyn_elf *rpnt;
419 handle = (struct dyn_elf *) vhandle;
421 /* First of all verify that we have a real handle
422 of some kind. Return NULL if not a valid handle. */
425 handle = _dl_symbol_tables;
426 else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
427 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
431 _dl_error_number = LD_BAD_HANDLE;
434 } else if (handle == RTLD_NEXT) {
436 * Try and locate the module we were called from - we
437 * need this so that we know where to start searching
438 * from. We never pass RTLD_NEXT down into the actual
439 * dynamic loader itself, as it doesn't know
440 * how to properly treat it.
442 from = (ElfW(Addr)) __builtin_return_address(0);
445 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
447 if (tpnt->loadaddr < from
448 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
455 ret = _dl_find_hash((char*)name, handle, NULL, 0);
461 _dl_error_number = LD_NO_SYMBOL;
465 static int do_dlclose(void *vhandle, int need_fini)
467 struct dyn_elf *rpnt, *rpnt1;
468 struct init_fini_list *runp, *tmp;
470 struct elf_resolve *tpnt, *run_tpnt;
471 int (*dl_elf_fini) (void);
472 void (*dl_brk) (void);
473 struct dyn_elf *handle;
477 handle = (struct dyn_elf *) vhandle;
478 if (handle == _dl_symbol_tables)
481 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
488 _dl_error_number = LD_BAD_HANDLE;
492 rpnt1->next_handle = rpnt->next_handle;
494 _dl_handles = rpnt->next_handle;
495 #ifdef __SUPPORT_LD_DEBUG__
497 fprintf(stderr, "dlclose: %s, usage count: %d\n", handle->dyn->libname, handle->dyn->usage_count);
499 if (handle->dyn->usage_count != 1) {
500 handle->dyn->usage_count--;
504 /* OK, this is a valid handle - now close out the file */
505 for (j = 0; j < handle->init_fini.nlist; ++j) {
506 tpnt = handle->init_fini.init_fini[j];
507 if (--tpnt->usage_count == 0) {
508 if (tpnt->dynamic_info[DT_FINI] && need_fini &&
509 !(tpnt->init_flag & FINI_FUNCS_CALLED)) {
510 tpnt->init_flag |= FINI_FUNCS_CALLED;
511 dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
512 #ifdef __SUPPORT_LD_DEBUG__
514 fprintf(stderr, "running dtors for library %s at '%x'\n", tpnt->libname, (unsigned)dl_elf_fini);
519 #ifdef __SUPPORT_LD_DEBUG__
521 fprintf(stderr, "dlclose 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 #ifdef __SUPPORT_LD_DEBUG__
548 fprintf(stderr, "dlclose removing loaded_modules: %s\n", tpnt->libname);
550 run_tpnt->next = run_tpnt->next->next;
552 run_tpnt->next->prev = run_tpnt;
556 /* Next, remove tpnt from the global symbol table list */
557 if (_dl_symbol_tables->dyn == tpnt) {
558 _dl_symbol_tables = _dl_symbol_tables->next;
559 if (_dl_symbol_tables)
560 _dl_symbol_tables->prev = 0;
562 for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
563 if (rpnt1->next->dyn == tpnt) {
564 #ifdef __SUPPORT_LD_DEBUG__
566 fprintf(stderr, "dlclose removing symbol_tables: %s\n", tpnt->libname);
569 rpnt1->next = rpnt1->next->next;
571 rpnt1->next->prev = rpnt1;
579 free(handle->init_fini.init_fini);
583 if (_dl_debug_addr) {
584 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
585 if (dl_brk != NULL) {
586 _dl_debug_addr->r_state = RT_DELETE;
589 _dl_debug_addr->r_state = RT_CONSISTENT;
597 int dlclose(void *vhandle)
599 return do_dlclose(vhandle, 1);
606 if (!_dl_error_number)
608 retval = dl_error_names[_dl_error_number];
609 _dl_error_number = 0;
610 return (char *)retval;
614 * Dump information to stderrr about the current loaded modules
616 static char *type[] = { "Lib", "Exe", "Int", "Mod" };
620 struct elf_resolve *tpnt;
621 struct dyn_elf *rpnt, *hpnt;
623 fprintf(stderr, "List of loaded modules\n");
624 /* First start with a complete list of all of the loaded files. */
625 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
626 fprintf(stderr, "\t%x %x %x %s %d %s\n",
627 (unsigned) tpnt->loadaddr, (unsigned) tpnt,
628 (unsigned) tpnt->symbol_scope,
630 tpnt->usage_count, tpnt->libname);
633 /* Next dump the module list for the application itself */
634 fprintf(stderr, "\nModules for application (%x):\n",
635 (unsigned) _dl_symbol_tables);
636 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
637 fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname);
639 for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
640 fprintf(stderr, "Modules for handle %x\n", (unsigned) hpnt);
641 for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
642 fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn,
648 int dladdr(const void *__address, Dl_info * __info)
650 struct elf_resolve *pelf;
651 struct elf_resolve *rpnt;
656 * Try and locate the module address is in
661 fprintf(stderr, "dladdr( %x, %x )\n", __address, __info);
664 for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
665 struct elf_resolve *tpnt;
669 fprintf(stderr, "Module \"%s\" at %x\n",
670 tpnt->libname, tpnt->loadaddr);
672 if (tpnt->loadaddr < (ElfW(Addr)) __address
673 && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
683 * Try and locate the symbol of address
695 symtab = (Elf32_Sym *) (pelf->dynamic_info[DT_SYMTAB]);
696 strtab = (char *) (pelf->dynamic_info[DT_STRTAB]);
699 for (hn = 0; hn < pelf->nbucket; hn++) {
700 for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
701 ElfW(Addr) symbol_addr;
703 symbol_addr = pelf->loadaddr + symtab[si].st_value;
704 if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
710 fprintf(stderr, "Symbol \"%s\" at %x\n",
711 strtab + symtab[si].st_name, symbol_addr);
717 __info->dli_fname = pelf->libname;
718 __info->dli_fbase = (void *)pelf->loadaddr;
719 __info->dli_sname = strtab + symtab[sn].st_name;
720 __info->dli_saddr = (void *)sa;