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"
90 #endif /* __LIBDL_SHARED__ */
92 #ifdef __SUPPORT_LD_DEBUG__
93 # define _dl_if_debug_print(fmt, args...) \
96 fprintf(stderr, "%s():%i: " fmt, __FUNCTION__, __LINE__, ## args); \
99 # define _dl_if_debug_print(fmt, args...)
102 static int do_dlclose(void *, int need_fini);
105 static const char *dl_error_names[] = {
108 "Unable to open /dev/zero",
110 #if defined (__i386__)
112 #elif defined (__sparc__)
114 #elif defined (__mc68000__)
117 "Unrecognized binary type",
119 "Not an ELF shared library",
120 "Unable to mmap file",
121 "No dynamic section",
122 #ifdef ELF_USES_RELOCA
123 "Unable to process REL relocs",
125 "Unable to process RELA relocs",
128 "Unable to resolve symbol"
131 void __attribute__ ((destructor)) 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;
143 struct dyn_elf *dpnt;
145 struct elf_resolve *tpnt1;
146 void (*dl_brk) (void);
148 struct init_fini_list *tmp, *runp, *runp2, *dep_list;
150 struct elf_resolve **init_fini_list;
152 /* A bit of sanity checking... */
153 if (!(flag & (RTLD_LAZY|RTLD_NOW))) {
154 _dl_error_number = LD_BAD_HANDLE;
158 from = (ElfW(Addr)) __builtin_return_address(0);
160 /* Cover the trivial case first */
162 return _dl_symbol_tables;
167 * Try and locate the module we were called from - we
168 * need this so that we get the correct RPATH/RUNPATH. Note that
169 * this is the current behavior under Solaris, but the
170 * ABI+ specifies that we should only use the RPATH from
171 * the application. Thus this may go away at some time
175 for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
177 if (tpnt->loadaddr < from
178 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr))
181 for(rpnt = _dl_symbol_tables; rpnt->next; rpnt=rpnt->next);
184 now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
185 if (getenv("LD_BIND_NOW"))
188 /* Try to load the specified library */
189 _dl_if_debug_print("Trying to dlopen '%s', RTLD_GLOBAL:%d RTLD_NOW:%d\n",
190 (char*)libname, (flag & RTLD_GLOBAL ? 1:0), (now_flag & RTLD_NOW ? 1:0));
191 tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname, 0);
197 dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
198 _dl_memset(dyn_chain, 0, sizeof(struct dyn_elf));
199 dyn_chain->dyn = tpnt;
200 tpnt->rtld_flags |= (flag & RTLD_GLOBAL);
202 dyn_chain->next_handle = _dl_handles;
203 _dl_handles = dyn_ptr = dyn_chain;
205 if (tpnt->usage_count > 1) {
206 _dl_if_debug_print("Lib: %s already opened\n", libname);
207 /* see if there is a handle from a earlier dlopen */
208 for (handle = _dl_handles->next_handle; handle; handle = handle->next_handle) {
209 if (handle->dyn == tpnt) {
210 dyn_chain->init_fini.init_fini = handle->init_fini.init_fini;
211 dyn_chain->init_fini.nlist = handle->init_fini.nlist;
212 for(i=0; i < dyn_chain->init_fini.nlist; i++)
213 dyn_chain->init_fini.init_fini[i]->rtld_flags |= (flag & RTLD_GLOBAL);
214 dyn_chain->next = handle->next;
220 tpnt->init_flag |= DL_OPENED;
223 _dl_if_debug_print("Looking for needed libraries\n");
225 runp = alloca(sizeof(*runp));
228 dep_list = runp2 = runp;
229 for (; runp; runp = runp->next)
235 runp->tpnt->init_fini = NULL; /* clear any previous dependcies */
236 for (dpnt = (Elf32_Dyn *) runp->tpnt->dynamic_addr; dpnt->d_tag; dpnt++) {
237 if (dpnt->d_tag == DT_NEEDED) {
240 lpntstr = (char*) (runp->tpnt->dynamic_info[DT_STRTAB] +
242 name = _dl_get_last_path_component(lpntstr);
243 _dl_if_debug_print("Trying to load '%s', needed by '%s'\n",
244 lpntstr, runp->tpnt->libname);
245 tpnt1 = _dl_load_shared_library(0, &rpnt, runp->tpnt, lpntstr, 0);
249 tpnt1->rtld_flags |= (flag & RTLD_GLOBAL);
251 if (tpnt1->usage_count == 1) {
252 tpnt1->init_flag |= DL_OPENED;
253 /* This list is for dlsym() and relocation */
254 dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
255 _dl_memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
256 dyn_ptr = dyn_ptr->next;
257 dyn_ptr->dyn = tpnt1;
259 if (tpnt1->init_flag & DL_OPENED) {
260 /* Used to record RTLD_LOCAL scope */
261 tmp = alloca(sizeof(struct init_fini_list));
263 tmp->next = runp->tpnt->init_fini;
264 runp->tpnt->init_fini = tmp;
266 for (tmp=dep_list; tmp; tmp = tmp->next) {
267 if (tpnt1 == tmp->tpnt) { /* if match => cirular dependency, drop it */
268 _dl_if_debug_print("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 _dl_if_debug_print("Move %s from pos %d to %d in INIT/FINI list.\n", here->libname, k, j);
312 for (i = (k - j); i; --i)
313 init_fini_list[i+j] = init_fini_list[i+j-1];
314 init_fini_list[j] = here;
321 #ifdef __SUPPORT_LD_DEBUG__
323 fprintf(stderr, "\nINIT/FINI order and dependencies:\n");
324 for (i=0;i < nlist;i++) {
325 fprintf(stderr, "lib: %s has deps:\n", init_fini_list[i]->libname);
326 runp = init_fini_list[i]->init_fini;
327 for (; runp; runp = runp->next)
328 printf(" %s ", runp->tpnt->libname);
334 _dl_if_debug_print("Beginning dlopen relocation fixups\n");
336 * OK, now all of the kids are tucked into bed in their proper addresses.
337 * Now we go through and look for REL and RELA records that indicate fixups
338 * to the GOT tables. We need to do this in reverse order so that COPY
339 * directives work correctly */
342 * Relocation of the GOT entries for MIPS have to be done
343 * after all the libraries have been loaded.
345 _dl_perform_mips_global_got_relocations(tpnt, !now_flag);
348 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);
355 /* TODO: Should we set the protections of all pages back to R/O now ? */
358 /* Notify the debugger we have added some objects. */
359 if (_dl_debug_addr) {
360 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
361 if (dl_brk != NULL) {
362 _dl_debug_addr->r_state = RT_ADD;
365 _dl_debug_addr->r_state = RT_CONSISTENT;
370 #if defined (__LIBDL_SHARED__)
371 /* Run the ctors and setup the dtors */
372 for (i = nlist; i; --i) {
373 tpnt = init_fini_list[i-1];
374 if (tpnt->init_flag & INIT_FUNCS_CALLED)
376 tpnt->init_flag |= INIT_FUNCS_CALLED;
378 if (tpnt->dynamic_info[DT_INIT]) {
379 void (*dl_elf_func) (void);
380 dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
381 if (dl_elf_func && *dl_elf_func != NULL) {
382 _dl_if_debug_print("running ctors for library %s at '%x'\n",
383 tpnt->libname, (unsigned)dl_elf_func);
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;
453 static int do_dlclose(void *vhandle, int need_fini)
455 struct dyn_elf *rpnt, *rpnt1;
456 struct init_fini_list *runp, *tmp;
458 struct elf_resolve *tpnt, *run_tpnt;
459 int (*dl_elf_fini) (void);
460 void (*dl_brk) (void);
461 struct dyn_elf *handle;
465 handle = (struct dyn_elf *) vhandle;
466 if (handle == _dl_symbol_tables)
469 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
476 _dl_error_number = LD_BAD_HANDLE;
480 rpnt1->next_handle = rpnt->next_handle;
482 _dl_handles = rpnt->next_handle;
483 _dl_if_debug_print("%s: usage count: %d\n",
484 handle->dyn->libname, handle->dyn->usage_count);
485 if (handle->dyn->usage_count != 1) {
486 handle->dyn->usage_count--;
490 /* OK, this is a valid handle - now close out the file */
491 for (j = 0; j < handle->init_fini.nlist; ++j) {
492 tpnt = handle->init_fini.init_fini[j];
493 if (--tpnt->usage_count == 0) {
494 if (tpnt->dynamic_info[DT_FINI] && need_fini &&
495 !(tpnt->init_flag & FINI_FUNCS_CALLED)) {
496 tpnt->init_flag |= FINI_FUNCS_CALLED;
497 dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
498 _dl_if_debug_print("running dtors for library %s at '%x'\n",
499 tpnt->libname, (unsigned)dl_elf_fini);
503 _dl_if_debug_print("unmapping: %s\n", tpnt->libname);
505 for (i = 0, ppnt = tpnt->ppnt;
506 i < tpnt->n_phent; ppnt++, i++) {
507 if (ppnt->p_type != PT_LOAD)
509 if (end < ppnt->p_vaddr + ppnt->p_memsz)
510 end = ppnt->p_vaddr + ppnt->p_memsz;
512 _dl_munmap((void*)tpnt->loadaddr, end);
513 /* Free elements in RTLD_LOCAL scope list */
514 for (runp = tpnt->rtld_local; runp; runp = tmp) {
519 /* Next, remove tpnt from the loaded_module list */
520 if (_dl_loaded_modules == tpnt) {
521 _dl_loaded_modules = tpnt->next;
522 if (_dl_loaded_modules)
523 _dl_loaded_modules->prev = 0;
525 for (run_tpnt = _dl_loaded_modules; run_tpnt; run_tpnt = run_tpnt->next)
526 if (run_tpnt->next == tpnt) {
527 _dl_if_debug_print("removing loaded_modules: %s\n", tpnt->libname);
528 run_tpnt->next = run_tpnt->next->next;
530 run_tpnt->next->prev = run_tpnt;
534 /* Next, remove tpnt from the global symbol table list */
535 if (_dl_symbol_tables->dyn == tpnt) {
536 _dl_symbol_tables = _dl_symbol_tables->next;
537 if (_dl_symbol_tables)
538 _dl_symbol_tables->prev = 0;
540 for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
541 if (rpnt1->next->dyn == tpnt) {
542 _dl_if_debug_print("removing symbol_tables: %s\n", tpnt->libname);
544 rpnt1->next = rpnt1->next->next;
546 rpnt1->next->prev = rpnt1;
554 free(handle->init_fini.init_fini);
558 if (_dl_debug_addr) {
559 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
560 if (dl_brk != NULL) {
561 _dl_debug_addr->r_state = RT_DELETE;
564 _dl_debug_addr->r_state = RT_CONSISTENT;
572 int dlclose(void *vhandle)
574 return do_dlclose(vhandle, 1);
581 if (!_dl_error_number)
583 retval = dl_error_names[_dl_error_number];
584 _dl_error_number = 0;
585 return (char *)retval;
589 * Dump information to stderrr about the current loaded modules
591 static char *type[] = { "Lib", "Exe", "Int", "Mod" };
595 struct elf_resolve *tpnt;
596 struct dyn_elf *rpnt, *hpnt;
598 fprintf(stderr, "List of loaded modules\n");
599 /* First start with a complete list of all of the loaded files. */
600 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
601 fprintf(stderr, "\t%x %x %x %s %d %s\n",
602 (unsigned) tpnt->loadaddr, (unsigned) tpnt,
603 (unsigned) tpnt->symbol_scope,
605 tpnt->usage_count, tpnt->libname);
608 /* Next dump the module list for the application itself */
609 fprintf(stderr, "\nModules for application (%x):\n",
610 (unsigned) _dl_symbol_tables);
611 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
612 fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname);
614 for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
615 fprintf(stderr, "Modules for handle %x\n", (unsigned) hpnt);
616 for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
617 fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn,
623 int dladdr(const void *__address, Dl_info * __info)
625 struct elf_resolve *pelf;
626 struct elf_resolve *rpnt;
631 * Try and locate the module address is in
636 fprintf(stderr, "dladdr( %x, %x )\n", __address, __info);
639 for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
640 struct elf_resolve *tpnt;
644 fprintf(stderr, "Module \"%s\" at %x\n",
645 tpnt->libname, tpnt->loadaddr);
647 if (tpnt->loadaddr < (ElfW(Addr)) __address
648 && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
658 * Try and locate the symbol of address
670 symtab = (Elf32_Sym *) (pelf->dynamic_info[DT_SYMTAB]);
671 strtab = (char *) (pelf->dynamic_info[DT_STRTAB]);
674 for (hn = 0; hn < pelf->nbucket; hn++) {
675 for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
676 ElfW(Addr) symbol_addr;
678 symbol_addr = pelf->loadaddr + symtab[si].st_value;
679 if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
685 fprintf(stderr, "Symbol \"%s\" at %x\n",
686 strtab + symtab[si].st_name, symbol_addr);
692 __info->dli_fname = pelf->libname;
693 __info->dli_fbase = (void *)pelf->loadaddr;
694 __info->dli_sname = strtab + symtab[sn].st_name;
695 __info->dli_saddr = (void *)sa;