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 "../ldso/dl-debug.c"
87 #include "dl-progname.h"
88 #include "../ldso/dl-hash.c"
89 #define _dl_trace_loaded_objects 0
90 #include "../ldso/dl-elf.c"
91 #endif /* __LIBDL_SHARED__ */
93 #ifdef __SUPPORT_LD_DEBUG__
94 # define _dl_if_debug_print(fmt, args...) \
97 fprintf(stderr, "%s():%i: " fmt, __FUNCTION__, __LINE__, ## args); \
100 # define _dl_if_debug_print(fmt, args...)
103 static int do_dlclose(void *, int need_fini);
106 static const char *dl_error_names[] = {
109 "Unable to open /dev/zero",
111 #if defined (__i386__)
113 #elif defined (__sparc__)
115 #elif defined (__mc68000__)
118 "Unrecognized binary type",
120 "Not an ELF shared library",
121 "Unable to mmap file",
122 "No dynamic section",
123 #ifdef ELF_USES_RELOCA
124 "Unable to process REL relocs",
126 "Unable to process RELA relocs",
129 "Unable to resolve symbol"
132 void __attribute__ ((destructor)) dl_cleanup(void)
135 for (d = _dl_handles; d; d = d->next_handle) {
140 void *dlopen(const char *libname, int flag)
142 struct elf_resolve *tpnt, *tfrom;
143 struct dyn_elf *dyn_chain, *rpnt = NULL, *dyn_ptr, *relro_ptr, *handle;
144 struct dyn_elf *dpnt;
146 struct elf_resolve *tpnt1;
147 void (*dl_brk) (void);
149 struct init_fini_list *tmp, *runp, *runp2, *dep_list;
151 struct elf_resolve **init_fini_list;
153 /* A bit of sanity checking... */
154 if (!(flag & (RTLD_LAZY|RTLD_NOW))) {
155 _dl_error_number = LD_BAD_HANDLE;
159 from = (ElfW(Addr)) __builtin_return_address(0);
161 /* Cover the trivial case first */
163 return _dl_symbol_tables;
168 * Try and locate the module we were called from - we
169 * need this so that we get the correct RPATH/RUNPATH. Note that
170 * this is the current behavior under Solaris, but the
171 * ABI+ specifies that we should only use the RPATH from
172 * the application. Thus this may go away at some time
176 for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
178 if (tpnt->loadaddr < from
179 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr))
182 for(rpnt = _dl_symbol_tables; rpnt && rpnt->next; rpnt=rpnt->next);
185 now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
186 if (getenv("LD_BIND_NOW"))
189 /* Try to load the specified library */
190 _dl_if_debug_print("Trying to dlopen '%s', RTLD_GLOBAL:%d RTLD_NOW:%d\n",
191 (char*)libname, (flag & RTLD_GLOBAL ? 1:0), (now_flag & RTLD_NOW ? 1:0));
192 tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname, 0);
198 dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
199 _dl_memset(dyn_chain, 0, sizeof(struct dyn_elf));
200 dyn_chain->dyn = tpnt;
201 tpnt->rtld_flags |= (flag & RTLD_GLOBAL);
203 dyn_chain->next_handle = _dl_handles;
204 _dl_handles = dyn_ptr = dyn_chain;
206 if (tpnt->usage_count > 1) {
207 _dl_if_debug_print("Lib: %s already opened\n", libname);
208 /* see if there is a handle from a earlier dlopen */
209 for (handle = _dl_handles->next_handle; handle; handle = handle->next_handle) {
210 if (handle->dyn == tpnt) {
211 dyn_chain->init_fini.init_fini = handle->init_fini.init_fini;
212 dyn_chain->init_fini.nlist = handle->init_fini.nlist;
213 for(i=0; i < dyn_chain->init_fini.nlist; i++)
214 dyn_chain->init_fini.init_fini[i]->rtld_flags |= (flag & RTLD_GLOBAL);
215 dyn_chain->next = handle->next;
221 tpnt->init_flag |= DL_OPENED;
224 _dl_if_debug_print("Looking for needed libraries\n");
226 runp = alloca(sizeof(*runp));
229 dep_list = runp2 = runp;
230 for (; runp; runp = runp->next)
236 runp->tpnt->init_fini = NULL; /* clear any previous dependcies */
237 for (dpnt = (ElfW(Dyn) *) runp->tpnt->dynamic_addr; dpnt->d_tag; dpnt++) {
238 if (dpnt->d_tag == DT_NEEDED) {
241 lpntstr = (char*) (runp->tpnt->dynamic_info[DT_STRTAB] +
243 name = _dl_get_last_path_component(lpntstr);
244 _dl_if_debug_print("Trying to load '%s', needed by '%s'\n",
245 lpntstr, runp->tpnt->libname);
246 tpnt1 = _dl_load_shared_library(0, &rpnt, runp->tpnt, lpntstr, 0);
250 tpnt1->rtld_flags |= (flag & RTLD_GLOBAL);
252 if (tpnt1->usage_count == 1) {
253 tpnt1->init_flag |= DL_OPENED;
254 /* This list is for dlsym() and relocation */
255 dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
256 _dl_memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
257 dyn_ptr = dyn_ptr->next;
258 dyn_ptr->dyn = tpnt1;
260 if (tpnt1->init_flag & DL_OPENED) {
261 /* Used to record RTLD_LOCAL scope */
262 tmp = alloca(sizeof(struct init_fini_list));
264 tmp->next = runp->tpnt->init_fini;
265 runp->tpnt->init_fini = tmp;
267 for (tmp=dep_list; tmp; tmp = tmp->next) {
268 if (tpnt1 == tmp->tpnt) { /* if match => cirular dependency, drop it */
269 _dl_if_debug_print("Circular dependency, skipping '%s',\n",
271 tpnt1->usage_count--;
275 if (!tmp) { /* Don't add if circular dependency detected */
276 runp2->next = alloca(sizeof(*runp));
285 init_fini_list = malloc(nlist * sizeof(struct elf_resolve *));
286 dyn_chain->init_fini.init_fini = init_fini_list;
287 dyn_chain->init_fini.nlist = nlist;
289 for (runp2 = dep_list; runp2; runp2 = runp2->next) {
290 init_fini_list[i++] = runp2->tpnt;
291 for(runp = runp2->tpnt->init_fini; runp; runp = runp->next){
292 if (!(runp->tpnt->rtld_flags & RTLD_GLOBAL)) {
293 tmp = malloc(sizeof(struct init_fini_list));
294 tmp->tpnt = runp->tpnt;
295 tmp->next = runp2->tpnt->rtld_local;
296 runp2->tpnt->rtld_local = tmp;
301 /* Sort the INIT/FINI list in dependency order. */
302 for (runp2 = dep_list; runp2; runp2 = runp2->next) {
304 for (j = 0; init_fini_list[j] != runp2->tpnt; ++j)
306 for (k = j + 1; k < nlist; ++k) {
307 struct init_fini_list *runp = init_fini_list[k]->init_fini;
309 for (; runp; runp = runp->next) {
310 if (runp->tpnt == runp2->tpnt) {
311 struct elf_resolve *here = init_fini_list[k];
312 _dl_if_debug_print("Move %s from pos %d to %d in INIT/FINI list.\n", here->libname, k, j);
313 for (i = (k - j); i; --i)
314 init_fini_list[i+j] = init_fini_list[i+j-1];
315 init_fini_list[j] = here;
322 #ifdef __SUPPORT_LD_DEBUG__
324 fprintf(stderr, "\nINIT/FINI order and dependencies:\n");
325 for (i=0;i < nlist;i++) {
326 fprintf(stderr, "lib: %s has deps:\n", init_fini_list[i]->libname);
327 runp = init_fini_list[i]->init_fini;
328 for (; runp; runp = runp->next)
329 printf(" %s ", runp->tpnt->libname);
335 _dl_if_debug_print("Beginning dlopen relocation fixups\n");
337 * OK, now all of the kids are tucked into bed in their proper addresses.
338 * Now we go through and look for REL and RELA records that indicate fixups
339 * to the GOT tables. We need to do this in reverse order so that COPY
340 * directives work correctly */
343 * Relocation of the GOT entries for MIPS have to be done
344 * after all the libraries have been loaded.
346 _dl_perform_mips_global_got_relocations(tpnt, !now_flag);
349 if (_dl_fixup(dyn_chain, now_flag))
353 for (rpnt = relro_ptr->next; rpnt; rpnt = rpnt->next) {
354 if (rpnt->dyn->relro_size)
355 _dl_protect_relro(rpnt->dyn);
358 /* TODO: Should we set the protections of all pages back to R/O now ? */
361 /* Notify the debugger we have added some objects. */
362 if (_dl_debug_addr) {
363 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
364 if (dl_brk != NULL) {
365 _dl_debug_addr->r_state = RT_ADD;
368 _dl_debug_addr->r_state = RT_CONSISTENT;
373 #if defined (__LIBDL_SHARED__)
374 /* Run the ctors and setup the dtors */
375 for (i = nlist; i; --i) {
376 tpnt = init_fini_list[i-1];
377 if (tpnt->init_flag & INIT_FUNCS_CALLED)
379 tpnt->init_flag |= INIT_FUNCS_CALLED;
381 if (tpnt->dynamic_info[DT_INIT]) {
382 void (*dl_elf_func) (void);
383 dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
384 if (dl_elf_func && *dl_elf_func != NULL) {
385 _dl_if_debug_print("running ctors for library %s at '%p'\n",
386 tpnt->libname, dl_elf_func);
393 return (void *) dyn_chain;
396 /* Something went wrong. Clean up and return NULL. */
398 do_dlclose(dyn_chain, 0);
402 void *dlsym(void *vhandle, const char *name)
404 struct elf_resolve *tpnt, *tfrom;
405 struct dyn_elf *handle;
407 struct dyn_elf *rpnt;
410 handle = (struct dyn_elf *) vhandle;
412 /* First of all verify that we have a real handle
413 of some kind. Return NULL if not a valid handle. */
416 handle = _dl_symbol_tables;
417 else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
418 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
422 _dl_error_number = LD_BAD_HANDLE;
425 } else if (handle == RTLD_NEXT) {
427 * Try and locate the module we were called from - we
428 * need this so that we know where to start searching
429 * from. We never pass RTLD_NEXT down into the actual
430 * dynamic loader itself, as it doesn't know
431 * how to properly treat it.
433 from = (ElfW(Addr)) __builtin_return_address(0);
436 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
438 if (tpnt->loadaddr < from
439 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
446 ret = _dl_find_hash((char*)name, handle, NULL, 0);
452 _dl_error_number = LD_NO_SYMBOL;
456 static int do_dlclose(void *vhandle, int need_fini)
458 struct dyn_elf *rpnt, *rpnt1;
459 struct init_fini_list *runp, *tmp;
461 struct elf_resolve *tpnt, *run_tpnt;
462 int (*dl_elf_fini) (void);
463 void (*dl_brk) (void);
464 struct dyn_elf *handle;
468 handle = (struct dyn_elf *) vhandle;
469 if (handle == _dl_symbol_tables)
472 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
479 _dl_error_number = LD_BAD_HANDLE;
483 rpnt1->next_handle = rpnt->next_handle;
485 _dl_handles = rpnt->next_handle;
486 _dl_if_debug_print("%s: usage count: %d\n",
487 handle->dyn->libname, handle->dyn->usage_count);
488 if (handle->dyn->usage_count != 1) {
489 handle->dyn->usage_count--;
493 /* OK, this is a valid handle - now close out the file */
494 for (j = 0; j < handle->init_fini.nlist; ++j) {
495 tpnt = handle->init_fini.init_fini[j];
496 if (--tpnt->usage_count == 0) {
497 if (tpnt->dynamic_info[DT_FINI] && need_fini &&
498 !(tpnt->init_flag & FINI_FUNCS_CALLED)) {
499 tpnt->init_flag |= FINI_FUNCS_CALLED;
500 dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
501 _dl_if_debug_print("running dtors for library %s at '%p'\n",
502 tpnt->libname, dl_elf_fini);
506 _dl_if_debug_print("unmapping: %s\n", tpnt->libname);
508 for (i = 0, ppnt = tpnt->ppnt;
509 i < tpnt->n_phent; ppnt++, i++) {
510 if (ppnt->p_type != PT_LOAD)
512 if (end < ppnt->p_vaddr + ppnt->p_memsz)
513 end = ppnt->p_vaddr + ppnt->p_memsz;
515 _dl_munmap((void*)tpnt->loadaddr, end);
516 /* Free elements in RTLD_LOCAL scope list */
517 for (runp = tpnt->rtld_local; runp; runp = tmp) {
522 /* Next, remove tpnt from the loaded_module list */
523 if (_dl_loaded_modules == tpnt) {
524 _dl_loaded_modules = tpnt->next;
525 if (_dl_loaded_modules)
526 _dl_loaded_modules->prev = 0;
528 for (run_tpnt = _dl_loaded_modules; run_tpnt; run_tpnt = run_tpnt->next)
529 if (run_tpnt->next == tpnt) {
530 _dl_if_debug_print("removing loaded_modules: %s\n", tpnt->libname);
531 run_tpnt->next = run_tpnt->next->next;
533 run_tpnt->next->prev = run_tpnt;
537 /* Next, remove tpnt from the global symbol table list */
538 if (_dl_symbol_tables) {
539 if (_dl_symbol_tables->dyn == tpnt) {
540 _dl_symbol_tables = _dl_symbol_tables->next;
541 if (_dl_symbol_tables)
542 _dl_symbol_tables->prev = 0;
544 for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
545 if (rpnt1->next->dyn == tpnt) {
546 _dl_if_debug_print("removing symbol_tables: %s\n", tpnt->libname);
548 rpnt1->next = rpnt1->next->next;
550 rpnt1->next->prev = rpnt1;
559 free(handle->init_fini.init_fini);
563 if (_dl_debug_addr) {
564 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
565 if (dl_brk != NULL) {
566 _dl_debug_addr->r_state = RT_DELETE;
569 _dl_debug_addr->r_state = RT_CONSISTENT;
577 int dlclose(void *vhandle)
579 return do_dlclose(vhandle, 1);
586 if (!_dl_error_number)
588 retval = dl_error_names[_dl_error_number];
589 _dl_error_number = 0;
590 return (char *)retval;
594 * Dump information to stderrr about the current loaded modules
596 static char *type[] = { "Lib", "Exe", "Int", "Mod" };
600 struct elf_resolve *tpnt;
601 struct dyn_elf *rpnt, *hpnt;
603 fprintf(stderr, "List of loaded modules\n");
604 /* First start with a complete list of all of the loaded files. */
605 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
606 fprintf(stderr, "\t%p %p %p %s %d %s\n",
607 tpnt->loadaddr, tpnt, tpnt->symbol_scope,
609 tpnt->usage_count, tpnt->libname);
612 /* Next dump the module list for the application itself */
613 fprintf(stderr, "\nModules for application (%p):\n", _dl_symbol_tables);
614 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
615 fprintf(stderr, "\t%p %s\n", rpnt->dyn, rpnt->dyn->libname);
617 for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
618 fprintf(stderr, "Modules for handle %p\n", hpnt);
619 for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
620 fprintf(stderr, "\t%p %s\n", rpnt->dyn, rpnt->dyn->libname);
625 int dladdr(const void *__address, Dl_info * __info)
627 struct elf_resolve *pelf;
628 struct elf_resolve *rpnt;
633 * Try and locate the module address is in
638 fprintf(stderr, "dladdr( %p, %p )\n", __address, __info);
641 for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
642 struct elf_resolve *tpnt;
646 fprintf(stderr, "Module \"%s\" at %p\n",
647 tpnt->libname, tpnt->loadaddr);
649 if (tpnt->loadaddr < (ElfW(Addr)) __address
650 && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
660 * Try and locate the symbol of address
672 symtab = (ElfW(Sym) *) (pelf->dynamic_info[DT_SYMTAB]);
673 strtab = (char *) (pelf->dynamic_info[DT_STRTAB]);
676 for (hn = 0; hn < pelf->nbucket; hn++) {
677 for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
678 ElfW(Addr) symbol_addr;
680 symbol_addr = pelf->loadaddr + symtab[si].st_value;
681 if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
687 fprintf(stderr, "Symbol \"%s\" at %p\n",
688 strtab + symtab[si].st_name, symbol_addr);
694 __info->dli_fname = pelf->libname;
695 __info->dli_fbase = (void *)pelf->loadaddr;
696 __info->dli_sname = strtab + symtab[sn].st_name;
697 __info->dli_saddr = (void *)sa;