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;
134 struct dyn_elf *dyn_chain, *rpnt = NULL, *dyn_ptr, *relro_ptr;
135 struct dyn_elf *dpnt;
137 struct elf_resolve *tpnt1;
138 void (*dl_brk) (void);
140 struct init_fini_list *tmp, *runp;
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);
191 dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
192 _dl_memset(dyn_chain, 0, sizeof(struct dyn_elf));
193 dyn_chain->dyn = tpnt;
194 tpnt->rtld_flags |= (flag & RTLD_GLOBAL);
196 dyn_chain->next_handle = _dl_handles;
197 _dl_handles = dyn_ptr = dyn_chain;
199 #ifdef __SUPPORT_LD_DEBUG__
201 fprintf(stderr, "Looking for needed libraries\n");
204 for (tcurr = tpnt; tcurr; tcurr = tcurr->next)
210 tcurr->init_fini = NULL; /* clear any previous dependcies */
211 for (dpnt = (Elf32_Dyn *) tcurr->dynamic_addr; dpnt->d_tag; dpnt++) {
212 if (dpnt->d_tag == DT_NEEDED) {
215 lpntstr = (char*) (tcurr->dynamic_info[DT_STRTAB] +
217 name = _dl_get_last_path_component(lpntstr);
218 tpnt1 = _dl_check_if_named_library_is_loaded(name, 0);
219 #ifdef __SUPPORT_LD_DEBUG__
221 fprintf(stderr, "Trying to load '%s', needed by '%s'\n",
222 lpntstr, tcurr->libname);
225 tpnt1->usage_count++;
227 tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpntstr, 0);
231 tpnt1->rtld_flags |= (flag & RTLD_GLOBAL);
232 dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
233 _dl_memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
234 dyn_ptr = dyn_ptr->next;
235 dyn_ptr->dyn = tpnt1;
237 tmp = alloca(sizeof(struct init_fini_list)); /* Allocates on stack, no need to free this memory */
239 tmp->next = tcurr->init_fini;
240 tcurr->init_fini = tmp;
244 init_fini_list = malloc(nlist * sizeof(struct elf_resolve *));
245 dyn_chain->init_fini.init_fini = init_fini_list;
246 dyn_chain->init_fini.nlist = nlist;
248 for (tcurr = tpnt; tcurr; tcurr = tcurr->next) {
249 init_fini_list[i++] = tcurr;
250 for(runp = tcurr->init_fini; runp; runp = runp->next){
251 if (!(runp->tpnt->rtld_flags & RTLD_GLOBAL)) {
252 tmp = malloc(sizeof(struct init_fini_list));
253 tmp->tpnt = runp->tpnt;
254 tmp->next = tcurr->rtld_local;
255 tcurr->rtld_local = tmp;
260 /* Sort the INIT/FINI list in dependency order. */
261 for (tcurr = tpnt; tcurr; tcurr = tcurr->next) {
263 for (j = 0; init_fini_list[j] != tcurr; ++j)
265 for (k = j + 1; k < nlist; ++k) {
266 struct init_fini_list *runp = init_fini_list[k]->init_fini;
268 for (; runp; runp = runp->next) {
269 if (runp->tpnt == tcurr) {
270 struct elf_resolve *here = init_fini_list[k];
271 #ifdef __SUPPORT_LD_DEBUG__
273 fprintf(stderr, "Move %s from pos %d to %d in INIT/FINI list.\n", here->libname, k, j);
275 for (i = (k - j); i; --i)
276 init_fini_list[i+j] = init_fini_list[i+j-1];
277 init_fini_list[j] = here;
284 #ifdef __SUPPORT_LD_DEBUG__
286 fprintf(stderr, "\nINIT/FINI order and dependencies:\n");
287 for (i=0;i < nlist;i++) {
288 fprintf(stderr, "lib: %s has deps:\n", init_fini_list[i]->libname);
289 runp = init_fini_list[i]->init_fini;
290 for ( ;runp; runp = runp->next)
291 printf(" %s ", runp->tpnt->libname);
297 if (dyn_chain->dyn->init_flag & INIT_FUNCS_CALLED) {
298 /* If the init and fini stuff has already been run, that means
299 * the dlopen'd library has already been loaded, and nothing
300 * further needs to be done. */
301 return (void *) dyn_chain;
304 #ifdef __SUPPORT_LD_DEBUG__
306 fprintf(stderr, "Beginning dlopen relocation fixups\n");
309 * OK, now all of the kids are tucked into bed in their proper addresses.
310 * Now we go through and look for REL and RELA records that indicate fixups
311 * to the GOT tables. We need to do this in reverse order so that COPY
312 * directives work correctly */
313 now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
314 if (getenv("LD_BIND_NOW"))
319 * Relocation of the GOT entries for MIPS have to be done
320 * after all the libraries have been loaded.
322 _dl_perform_mips_global_got_relocations(tpnt, !now_flag);
325 if (_dl_fixup(dyn_chain, now_flag))
328 for (rpnt = relro_ptr->next; rpnt; rpnt = rpnt->next) {
329 if (rpnt->dyn->relro_size)
330 _dl_protect_relro(rpnt->dyn);
332 /* TODO: Should we set the protections of all pages back to R/O now ? */
335 /* Notify the debugger we have added some objects. */
336 if (_dl_debug_addr) {
337 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
338 if (dl_brk != NULL) {
339 _dl_debug_addr->r_state = RT_ADD;
342 _dl_debug_addr->r_state = RT_CONSISTENT;
347 #if defined (__LIBDL_SHARED__)
348 /* Run the ctors and setup the dtors */
349 for (i = nlist; i; --i) {
350 tpnt = init_fini_list[i-1];
351 if (tpnt->init_flag & INIT_FUNCS_CALLED)
353 tpnt->init_flag |= INIT_FUNCS_CALLED;
355 if (tpnt->dynamic_info[DT_INIT]) {
356 void (*dl_elf_func) (void);
357 dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
358 if (dl_elf_func && *dl_elf_func != NULL) {
359 #ifdef __SUPPORT_LD_DEBUG__
361 fprintf(stderr, "running ctors for library %s at '%x'\n", tpnt->libname, (unsigned)dl_elf_func);
369 return (void *) dyn_chain;
372 /* Something went wrong. Clean up and return NULL. */
374 do_dlclose(dyn_chain, 0);
378 void *dlsym(void *vhandle, const char *name)
380 struct elf_resolve *tpnt, *tfrom;
381 struct dyn_elf *handle;
383 struct dyn_elf *rpnt;
386 handle = (struct dyn_elf *) vhandle;
388 /* First of all verify that we have a real handle
389 of some kind. Return NULL if not a valid handle. */
392 handle = _dl_symbol_tables;
393 else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
394 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
398 _dl_error_number = LD_BAD_HANDLE;
401 } else if (handle == RTLD_NEXT) {
403 * Try and locate the module we were called from - we
404 * need this so that we know where to start searching
405 * from. We never pass RTLD_NEXT down into the actual
406 * dynamic loader itself, as it doesn't know
407 * how to properly treat it.
409 from = (ElfW(Addr)) __builtin_return_address(0);
412 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
414 if (tpnt->loadaddr < from
415 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
422 ret = _dl_find_hash((char*)name, handle, NULL, 0);
428 _dl_error_number = LD_NO_SYMBOL;
432 static int do_dlclose(void *vhandle, int need_fini)
434 struct dyn_elf *rpnt, *rpnt1;
435 struct init_fini_list *runp, *tmp;
437 struct elf_resolve *tpnt;
438 int (*dl_elf_fini) (void);
439 void (*dl_brk) (void);
440 struct dyn_elf *handle;
444 handle = (struct dyn_elf *) vhandle;
446 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
453 _dl_error_number = LD_BAD_HANDLE;
457 rpnt1->next_handle = rpnt->next_handle;
459 _dl_handles = rpnt->next_handle;
461 for (i = 0; i < handle->init_fini.nlist; ++i) {
462 tpnt = handle->init_fini.init_fini[i];
463 if (tpnt->dynamic_info[DT_FINI] && tpnt->usage_count == 1 &&
464 !(tpnt->init_flag & FINI_FUNCS_CALLED)) {
465 tpnt->init_flag |= FINI_FUNCS_CALLED;
466 dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
467 #ifdef __SUPPORT_LD_DEBUG__
469 fprintf(stderr, "running dtors for library %s at '%x'\n", tpnt->libname, (unsigned)dl_elf_fini);
475 if (handle->dyn->usage_count == 1)
476 free(handle->init_fini.init_fini);
477 /* OK, this is a valid handle - now close out the file */
478 for (rpnt = handle; rpnt; rpnt = rpnt->next) {
480 if (--tpnt->usage_count == 0) {
482 for (i = 0, ppnt = tpnt->ppnt;
483 i < tpnt->n_phent; ppnt++, i++) {
484 if (ppnt->p_type != PT_LOAD)
486 if (end < ppnt->p_vaddr + ppnt->p_memsz)
487 end = ppnt->p_vaddr + ppnt->p_memsz;
489 _dl_munmap((void*)tpnt->loadaddr, end);
490 /* Free elements in RTLD_LOCAL scope list */
491 for (runp = tpnt->rtld_local; runp; runp = tmp) {
495 /* Next, remove tpnt from the loaded_module list */
496 if (_dl_loaded_modules == tpnt) {
497 _dl_loaded_modules = tpnt->next;
498 if (_dl_loaded_modules)
499 _dl_loaded_modules->prev = 0;
501 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
502 if (tpnt->next == rpnt->dyn) {
503 tpnt->next = tpnt->next->next;
505 tpnt->next->prev = tpnt;
509 /* Next, remove tpnt from the global symbol table list */
510 if (_dl_symbol_tables->dyn == rpnt->dyn) {
511 _dl_symbol_tables = rpnt->next;
512 if (_dl_symbol_tables)
513 _dl_symbol_tables->prev = 0;
515 for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
516 if (rpnt1->next->dyn == rpnt->dyn) {
518 rpnt1->next = rpnt1->next->next;
520 rpnt1->next->prev = rpnt1;
524 free(rpnt->dyn->libname);
531 if (_dl_debug_addr) {
532 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
533 if (dl_brk != NULL) {
534 _dl_debug_addr->r_state = RT_DELETE;
537 _dl_debug_addr->r_state = RT_CONSISTENT;
545 int dlclose(void *vhandle)
547 return do_dlclose(vhandle, 1);
554 if (!_dl_error_number)
556 retval = dl_error_names[_dl_error_number];
557 _dl_error_number = 0;
558 return (char *)retval;
562 * Dump information to stderrr about the current loaded modules
564 static char *type[] = { "Lib", "Exe", "Int", "Mod" };
568 struct elf_resolve *tpnt;
569 struct dyn_elf *rpnt, *hpnt;
571 fprintf(stderr, "List of loaded modules\n");
572 /* First start with a complete list of all of the loaded files. */
573 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
574 fprintf(stderr, "\t%x %x %x %s %d %s\n",
575 (unsigned) tpnt->loadaddr, (unsigned) tpnt,
576 (unsigned) tpnt->symbol_scope,
578 tpnt->usage_count, tpnt->libname);
581 /* Next dump the module list for the application itself */
582 fprintf(stderr, "\nModules for application (%x):\n",
583 (unsigned) _dl_symbol_tables);
584 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
585 fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname);
587 for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
588 fprintf(stderr, "Modules for handle %x\n", (unsigned) hpnt);
589 for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
590 fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn,
596 int dladdr(const void *__address, Dl_info * __info)
598 struct elf_resolve *pelf;
599 struct elf_resolve *rpnt;
604 * Try and locate the module address is in
609 fprintf(stderr, "dladdr( %x, %x )\n", __address, __info);
612 for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
613 struct elf_resolve *tpnt;
617 fprintf(stderr, "Module \"%s\" at %x\n",
618 tpnt->libname, tpnt->loadaddr);
620 if (tpnt->loadaddr < (ElfW(Addr)) __address
621 && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
631 * Try and locate the symbol of address
643 symtab = (Elf32_Sym *) (pelf->dynamic_info[DT_SYMTAB]);
644 strtab = (char *) (pelf->dynamic_info[DT_STRTAB]);
647 for (hn = 0; hn < pelf->nbucket; hn++) {
648 for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
649 ElfW(Addr) symbol_addr;
651 symbol_addr = pelf->loadaddr + symtab[si].st_value;
652 if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
658 fprintf(stderr, "Symbol \"%s\" at %x\n",
659 strtab + symtab[si].st_name, symbol_addr);
665 __info->dli_fname = pelf->libname;
666 __info->dli_fbase = (void *)pelf->loadaddr;
667 __info->dli_sname = strtab + symtab[sn].st_name;
668 __info->dli_saddr = (void *)sa;