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@codpoet.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
37 #if defined (__LIBDL_SHARED__)
39 /* When libdl is loaded as a shared library, we need to load in
40 * and use a pile of symbols from ldso... */
42 extern char *_dl_find_hash(const char *, struct dyn_elf *, int)
43 __attribute__ ((__weak__));
44 extern struct elf_resolve * _dl_load_shared_library(int, struct dyn_elf **,
45 struct elf_resolve *, char *, int) __attribute__ ((__weak__));
46 extern struct elf_resolve * _dl_check_if_named_library_is_loaded(const char *, int)
47 __attribute__ ((__weak__));
48 extern int _dl_fixup(struct dyn_elf *rpnt, int lazy)
49 __attribute__ ((__weak__));
50 extern int _dl_errno __attribute__ ((__weak__));
51 extern struct dyn_elf *_dl_symbol_tables __attribute__ ((__weak__));
52 extern struct dyn_elf *_dl_handles __attribute__ ((__weak__));
53 extern struct elf_resolve *_dl_loaded_modules __attribute__ ((__weak__));
54 extern struct r_debug *_dl_debug_addr __attribute__ ((__weak__));
55 extern unsigned long _dl_error_number __attribute__ ((__weak__));
56 extern void *(*_dl_malloc_function)(size_t) __attribute__ ((__weak__));
58 int _dl_map_cache(void) __attribute__ ((__weak__));
59 int _dl_unmap_cache(void) __attribute__ ((__weak__));
62 extern void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt)
63 __attribute__ ((__weak__));
65 #ifdef __SUPPORT_LD_DEBUG__
66 extern char *_dl_debug __attribute__ ((__weak__));
70 #else /* __LIBDL_SHARED__ */
72 /* When libdl is linked as a static library, we need to replace all
73 * the symbols that otherwise would have been loaded in from ldso... */
75 #ifdef __SUPPORT_LD_DEBUG__
78 char *_dl_library_path = 0; /* Where we look for libraries */
79 char *_dl_ldsopath = 0; /* Location of the shared lib loader */
80 int _dl_errno = 0; /* We can't use the real errno in ldso */
81 size_t _dl_pagesize = PAGE_SIZE; /* Store the page size for use later */
82 /* This global variable is also to communicate with debuggers such as gdb. */
83 struct r_debug *_dl_debug_addr = NULL;
84 #define _dl_malloc malloc
85 #include "dl-progname.h"
86 #include "../ldso/dl-hash.c"
87 #define _dl_trace_loaded_objects 0
88 #include "../ldso/dl-elf.c"
91 static int do_dlclose(void *, int need_fini);
94 static const char *dl_error_names[] = {
97 "Unable to open /dev/zero",
99 #if defined (__i386__)
101 #elif defined (__sparc__)
103 #elif defined (__mc68000__)
106 "Unrecognized binary type",
108 "Not an ELF shared library",
109 "Unable to mmap file",
110 "No dynamic section",
111 #ifdef ELF_USES_RELOCA
112 "Unable to process REL relocs",
114 "Unable to process RELA relocs",
117 "Unable to resolve symbol"
120 static void __attribute__ ((destructor)) dl_cleanup(void)
124 for (d = _dl_handles; d; d = d->next_handle)
125 if (d->dyn->libtype == loaded_file && d->dyn->dynamic_info[DT_FINI]) {
126 (* ((int (*)(void)) (d->dyn->loadaddr + d->dyn->dynamic_info[DT_FINI]))) ();
127 d->dyn->dynamic_info[DT_FINI] = 0;
131 void *dlopen(const char *libname, int flag)
133 struct elf_resolve *tpnt, *tfrom, *tcurr;
134 struct dyn_elf *dyn_chain, *rpnt = NULL;
135 struct dyn_elf *dpnt;
136 static int dl_init = 0;
138 struct elf_resolve *tpnt1;
139 void (*dl_brk) (void);
141 /* A bit of sanity checking... */
142 if (!(flag & (RTLD_LAZY|RTLD_NOW))) {
143 _dl_error_number = LD_BAD_HANDLE;
147 from = (ElfW(Addr)) __builtin_return_address(0);
149 /* Have the dynamic linker use the regular malloc function now */
152 #if defined (__LIBDL_SHARED__)
153 _dl_malloc_function = malloc;
157 /* Cover the trivial case first */
159 return _dl_symbol_tables;
164 * Try and locate the module we were called from - we
165 * need this so that we get the correct RPATH. Note that
166 * this is the current behavior under Solaris, but the
167 * ABI+ specifies that we should only use the RPATH from
168 * the application. Thus this may go away at some time
172 for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
174 if (tpnt->loadaddr < from
175 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr))
179 /* Try to load the specified library */
180 #ifdef __SUPPORT_LD_DEBUG__
182 fprintf(stderr, "Trying to dlopen '%s'\n", (char*)libname);
184 if (!(tpnt = _dl_check_if_named_library_is_loaded((char *)libname, 0)))
185 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 dyn_chain->flags = flag;
195 if (!tpnt->symbol_scope)
196 tpnt->symbol_scope = dyn_chain;
198 dyn_chain->next_handle = _dl_handles;
199 _dl_handles = rpnt = dyn_chain;
201 if (tpnt->init_flag & INIT_FUNCS_CALLED) {
202 /* If the init and fini stuff has already been run, that means
203 * the dlopen'd library has already been loaded, and nothing
204 * further needs to be done. */
205 return (void *) dyn_chain;
209 #ifdef __SUPPORT_LD_DEBUG__
211 fprintf(stderr, "Looking for needed libraries\n");
214 for (tcurr = tpnt; tcurr; tcurr = tcurr->next)
218 for (dpnt = (Elf32_Dyn *) tcurr->dynamic_addr; dpnt->d_tag; dpnt++) {
219 if (dpnt->d_tag == DT_NEEDED) {
222 lpntstr = (char*) (tcurr->loadaddr + tcurr->dynamic_info[DT_STRTAB] +
224 name = _dl_get_last_path_component(lpntstr);
226 if ((tpnt1 = _dl_check_if_named_library_is_loaded(name, 0)))
229 #ifdef __SUPPORT_LD_DEBUG__
231 fprintf(stderr, "Trying to load '%s', needed by '%s'\n",
232 lpntstr, tcurr->libname);
235 if (!(tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpntstr, 0))) {
239 rpnt->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
240 _dl_memset (rpnt->next, 0, sizeof (struct dyn_elf));
242 if (!tpnt1->symbol_scope) tpnt1->symbol_scope = rpnt;
250 * OK, now attach the entire chain at the end
252 rpnt->next = _dl_symbol_tables;
256 * Relocation of the GOT entries for MIPS have to be done
257 * after all the libraries have been loaded.
259 _dl_perform_mips_global_got_relocations(tpnt);
262 #ifdef __SUPPORT_LD_DEBUG__
264 fprintf(stderr, "Beginning dlopen relocation fixups\n");
267 * OK, now all of the kids are tucked into bed in their proper addresses.
268 * Now we go through and look for REL and RELA records that indicate fixups
269 * to the GOT tables. We need to do this in reverse order so that COPY
270 * directives work correctly */
271 if (_dl_fixup(dyn_chain, dyn_chain->flags))
274 /* TODO: Should we set the protections of all pages back to R/O now ? */
277 /* Notify the debugger we have added some objects. */
278 if (_dl_debug_addr) {
279 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
280 if (dl_brk != NULL) {
281 _dl_debug_addr->r_state = RT_ADD;
284 _dl_debug_addr->r_state = RT_CONSISTENT;
289 #if defined (__LIBDL_SHARED__)
290 /* Find the last library so we can run things in the right order */
291 for (tpnt = dyn_chain->dyn; tpnt->next!=NULL; tpnt = tpnt->next)
294 /* Run the ctors and set up the dtors */
295 for (; tpnt != dyn_chain->dyn->prev; tpnt=tpnt->prev)
297 /* Apparently crt1 for the application is responsible for handling this.
298 * We only need to run the init/fini for shared libraries
300 if (tpnt->libtype == program_interpreter)
302 if (tpnt->libtype == elf_executable)
304 if (tpnt->init_flag & INIT_FUNCS_CALLED)
306 tpnt->init_flag |= INIT_FUNCS_CALLED;
308 if (tpnt->dynamic_info[DT_INIT]) {
309 void (*dl_elf_func) (void);
310 dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
311 if (dl_elf_func && *dl_elf_func != NULL) {
312 #ifdef __SUPPORT_LD_DEBUG__
314 fprintf(stderr, "running ctors for library %s at '%x'\n", tpnt->libname, dl_elf_func);
319 if (tpnt->dynamic_info[DT_FINI]) {
320 void (*dl_elf_func) (void);
321 dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
322 if (dl_elf_func && *dl_elf_func != NULL) {
323 #ifdef __SUPPORT_LD_DEBUG__
325 fprintf(stderr, "setting up dtors for library %s at '%x'\n", tpnt->libname, dl_elf_func);
332 return (void *) dyn_chain;
335 /* Something went wrong. Clean up and return NULL. */
337 do_dlclose(dyn_chain, 0);
341 void *dlsym(void *vhandle, const char *name)
343 struct elf_resolve *tpnt, *tfrom;
344 struct dyn_elf *handle;
346 struct dyn_elf *rpnt;
349 handle = (struct dyn_elf *) vhandle;
351 /* First of all verify that we have a real handle
352 of some kind. Return NULL if not a valid handle. */
355 handle = _dl_symbol_tables;
356 else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
357 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
361 _dl_error_number = LD_BAD_HANDLE;
364 } else if (handle == RTLD_NEXT) {
366 * Try and locate the module we were called from - we
367 * need this so that we know where to start searching
368 * from. We never pass RTLD_NEXT down into the actual
369 * dynamic loader itself, as it doesn't know
370 * how to properly treat it.
372 from = (ElfW(Addr)) __builtin_return_address(0);
375 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
377 if (tpnt->loadaddr < from
378 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
385 ret = _dl_find_hash((char*)name, handle, 0);
391 _dl_error_number = LD_NO_SYMBOL;
395 static int do_dlclose(void *vhandle, int need_fini)
397 struct dyn_elf *rpnt, *rpnt1;
398 struct dyn_elf *spnt, *spnt1;
400 struct elf_resolve *tpnt;
401 int (*dl_elf_fini) (void);
402 void (*dl_brk) (void);
403 struct dyn_elf *handle;
407 handle = (struct dyn_elf *) vhandle;
409 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
410 if (rpnt == handle) {
417 _dl_error_number = LD_BAD_HANDLE;
421 /* OK, this is a valid handle - now close out the file.
422 * We check if we need to call fini () on the handle. */
423 spnt = need_fini ? handle : handle->next;
424 for (; spnt; spnt = spnt1) {
427 /* We appended the module list to the end - when we get back here,
428 quit. The access counts were not adjusted to account for being here. */
429 if (spnt == _dl_symbol_tables)
431 if (spnt->dyn->usage_count == 1
432 && spnt->dyn->libtype == loaded_file) {
434 /* Apparently crt1 for the application is responsible for handling this.
435 * We only need to run the init/fini for shared libraries
438 if (tpnt->dynamic_info[DT_FINI]) {
439 dl_elf_fini = (int (*)(void)) (tpnt->loadaddr +
440 tpnt->dynamic_info[DT_FINI]);
446 rpnt1->next_handle = rpnt->next_handle;
448 _dl_handles = rpnt->next_handle;
450 /* OK, this is a valid handle - now close out the file */
451 for (rpnt = handle; rpnt; rpnt = rpnt1) {
454 /* We appended the module list to the end - when we get back here,
455 quit. The access counts were not adjusted to account for being here. */
456 if (rpnt == _dl_symbol_tables)
459 rpnt->dyn->usage_count--;
460 if (rpnt->dyn->usage_count == 0
461 && rpnt->dyn->libtype == loaded_file) {
463 /* Apparently crt1 for the application is responsible for handling this.
464 * We only need to run the init/fini for shared libraries
468 /* We have to do this above, before we start closing objects.
469 * Otherwise when the needed symbols for _fini handling are
470 * resolved a coredump would occur. Rob Ryan (robr@cmu.edu)*/
471 if (tpnt->dynamic_info[DT_FINI]) {
472 dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
477 for (i = 0, ppnt = rpnt->dyn->ppnt;
478 i < rpnt->dyn->n_phent; ppnt++, i++) {
479 if (ppnt->p_type != PT_LOAD)
481 if (end < ppnt->p_vaddr + ppnt->p_memsz)
482 end = ppnt->p_vaddr + ppnt->p_memsz;
484 _dl_munmap((void*)rpnt->dyn->loadaddr, end);
485 /* Next, remove rpnt->dyn from the loaded_module list */
486 if (_dl_loaded_modules == rpnt->dyn) {
487 _dl_loaded_modules = rpnt->dyn->next;
488 if (_dl_loaded_modules)
489 _dl_loaded_modules->prev = 0;
491 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
492 if (tpnt->next == rpnt->dyn) {
493 tpnt->next = tpnt->next->next;
495 tpnt->next->prev = tpnt;
498 free(rpnt->dyn->libname);
505 if (_dl_debug_addr) {
506 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
507 if (dl_brk != NULL) {
508 _dl_debug_addr->r_state = RT_DELETE;
511 _dl_debug_addr->r_state = RT_CONSISTENT;
519 int dlclose(void *vhandle)
521 return do_dlclose(vhandle, 1);
524 const char *dlerror(void)
528 if (!_dl_error_number)
530 retval = dl_error_names[_dl_error_number];
531 _dl_error_number = 0;
536 * Dump information to stderrr about the current loaded modules
538 static char *type[] = { "Lib", "Exe", "Int", "Mod" };
542 struct elf_resolve *tpnt;
543 struct dyn_elf *rpnt, *hpnt;
545 fprintf(stderr, "List of loaded modules\n");
546 /* First start with a complete list of all of the loaded files. */
547 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
548 fprintf(stderr, "\t%x %x %x %s %d %s\n",
549 (unsigned) tpnt->loadaddr, (unsigned) tpnt,
550 (unsigned) tpnt->symbol_scope,
552 tpnt->usage_count, tpnt->libname);
555 /* Next dump the module list for the application itself */
556 fprintf(stderr, "\nModules for application (%x):\n",
557 (unsigned) _dl_symbol_tables);
558 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
559 fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname);
561 for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
562 fprintf(stderr, "Modules for handle %x\n", (unsigned) hpnt);
563 for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
564 fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn,
569 int dladdr(void *__address, Dl_info * __dlip)
571 struct elf_resolve *pelf;
572 struct elf_resolve *rpnt;
577 * Try and locate the module address is in
582 fprintf(stderr, "dladdr( %x, %x )\n", __address, __dlip);
585 for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
586 struct elf_resolve *tpnt;
590 fprintf(stderr, "Module \"%s\" at %x\n",
591 tpnt->libname, tpnt->loadaddr);
593 if (tpnt->loadaddr < (ElfW(Addr)) __address
594 && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
604 * Try and locate the symbol of address
616 symtab = (Elf32_Sym *) (pelf->dynamic_info[DT_SYMTAB] + pelf->loadaddr);
617 strtab = (char *) (pelf->dynamic_info[DT_STRTAB] + pelf->loadaddr);
620 for (hn = 0; hn < pelf->nbucket; hn++) {
621 for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
622 ElfW(Addr) symbol_addr;
624 symbol_addr = pelf->loadaddr + symtab[si].st_value;
625 if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
631 fprintf(stderr, "Symbol \"%s\" at %x\n",
632 strtab + symtab[si].st_name, symbol_addr);
638 __dlip->dli_fname = pelf->libname;
639 __dlip->dli_fbase = (void *)pelf->loadaddr;
640 __dlip->dli_sname = strtab + symtab[sn].st_name;
641 __dlip->dli_saddr = (void *)sa;