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
36 #if defined (__LIBDL_SHARED__)
38 /* When libdl is loaded as a shared library, we need to load in
39 * and use a pile of symbols from ldso... */
41 extern void _dl_dprintf(int, const char *, ...) __attribute__ ((__weak__));
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__));
57 extern void (*_dl_free_function) (void *p) __attribute__ ((__weak__));
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)
64 __attribute__ ((__weak__));
66 #ifdef __SUPPORT_LD_DEBUG__
67 extern char *_dl_debug __attribute__ ((__weak__));
68 extern char *_dl_debug_symbols __attribute__ ((__weak__));
69 extern char *_dl_debug_move __attribute__ ((__weak__));
70 extern char *_dl_debug_reloc __attribute__ ((__weak__));
71 extern char *_dl_debug_detail __attribute__ ((__weak__));
72 extern char *_dl_debug_nofixups __attribute__ ((__weak__));
73 extern char *_dl_debug_bindings __attribute__ ((__weak__));
74 extern int _dl_debug_file __attribute__ ((__weak__));
78 #else /* __LIBDL_SHARED__ */
80 /* When libdl is linked as a static library, we need to replace all
81 * the symbols that otherwise would have been loaded in from ldso... */
83 #ifdef __SUPPORT_LD_DEBUG__
85 char *_dl_debug_symbols = 0;
86 char *_dl_debug_move = 0;
87 char *_dl_debug_reloc = 0;
88 char *_dl_debug_detail = 0;
89 char *_dl_debug_nofixups = 0;
90 char *_dl_debug_bindings = 0;
91 int _dl_debug_file = 2;
93 char *_dl_library_path = 0;
94 char *_dl_ldsopath = 0;
95 struct r_debug *_dl_debug_addr = NULL;
96 static unsigned char *_dl_malloc_addr, *_dl_mmap_zero;
97 void *(*_dl_malloc_function) (size_t size);
99 void (*_dl_free_function) (void *p);
100 int _dl_fixup(struct dyn_elf *rpnt, int lazy);
101 #include "../ldso/dl-progname.h" /* Pull in the name of ld.so */
102 #include "../ldso/dl-hash.c"
103 #define _dl_trace_loaded_objects 0
104 #include "../ldso/dl-elf.c"
107 static int do_dlclose(void *, int need_fini);
110 static const char *dl_error_names[] = {
113 "Unable to open /dev/zero",
115 #if defined (__i386__)
117 #elif defined (__sparc__)
119 #elif defined (__mc68000__)
122 "Unrecognized binary type",
124 "Not an ELF shared library",
125 "Unable to mmap file",
126 "No dynamic section",
127 #ifdef ELF_USES_RELOCA
128 "Unable to process REL relocs",
130 "Unable to process RELA relocs",
133 "Unable to resolve symbol"
136 static void __attribute__ ((destructor)) dl_cleanup(void)
140 for (d = _dl_handles; d; d = d->next_handle)
141 if (d->dyn->libtype == loaded_file && d->dyn->dynamic_info[DT_FINI]) {
142 (* ((int (*)(void)) (d->dyn->loadaddr + d->dyn->dynamic_info[DT_FINI]))) ();
143 d->dyn->dynamic_info[DT_FINI] = 0;
147 void *_dlopen(const char *libname, int flag)
149 struct elf_resolve *tpnt, *tfrom, *tcurr;
150 struct dyn_elf *dyn_chain, *rpnt = NULL;
151 struct dyn_elf *dpnt;
152 static int dl_init = 0;
154 struct elf_resolve *tpnt1;
155 void (*dl_brk) (void);
157 /* A bit of sanity checking... */
158 if (!(flag & (RTLD_LAZY|RTLD_NOW))) {
159 _dl_error_number = LD_BAD_HANDLE;
163 from = (ElfW(Addr)) __builtin_return_address(0);
165 /* Have the dynamic linker use the regular malloc function now */
168 _dl_malloc_function = malloc;
169 _dl_free_function = free;
172 /* Cover the trivial case first */
174 return _dl_symbol_tables;
179 * Try and locate the module we were called from - we
180 * need this so that we get the correct RPATH. Note that
181 * this is the current behavior under Solaris, but the
182 * ABI+ specifies that we should only use the RPATH from
183 * the application. Thus this may go away at some time
187 for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
189 if (tpnt->loadaddr < from
190 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr))
194 /* Try to load the specified library */
195 #ifdef __SUPPORT_LD_DEBUG__
197 _dl_dprintf(_dl_debug_file, "Trying to dlopen '%s'\n", (char*)libname);
199 if (!(tpnt = _dl_check_if_named_library_is_loaded((char *)libname, 0)))
200 tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname, 0);
206 dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
207 _dl_memset(dyn_chain, 0, sizeof(struct dyn_elf));
208 dyn_chain->dyn = tpnt;
209 dyn_chain->flags = flag;
210 if (!tpnt->symbol_scope)
211 tpnt->symbol_scope = dyn_chain;
213 dyn_chain->next_handle = _dl_handles;
214 _dl_handles = rpnt = dyn_chain;
216 if (tpnt->init_flag & INIT_FUNCS_CALLED) {
217 /* If the init and fini stuff has already been run, that means
218 * the dlopen'd library has already been loaded, and nothing
219 * further needs to be done. */
220 return (void *) dyn_chain;
224 #ifdef __SUPPORT_LD_DEBUG__
226 _dl_dprintf(_dl_debug_file, "Looking for needed libraries\n");
229 for (tcurr = tpnt; tcurr; tcurr = tcurr->next)
233 for (dpnt = (Elf32_Dyn *) tcurr->dynamic_addr; dpnt->d_tag; dpnt++) {
234 if (dpnt->d_tag == DT_NEEDED) {
237 lpntstr = (char*) (tcurr->loadaddr + tcurr->dynamic_info[DT_STRTAB] +
239 name = _dl_get_last_path_component(lpntstr);
241 if ((tpnt1 = _dl_check_if_named_library_is_loaded(name, 0)))
244 #ifdef __SUPPORT_LD_DEBUG__
246 _dl_dprintf(_dl_debug_file, "Trying to load '%s', needed by '%s'\n",
247 lpntstr, tcurr->libname);
250 if (!(tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpntstr, 0))) {
254 rpnt->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
255 _dl_memset (rpnt->next, 0, sizeof (struct dyn_elf));
257 if (!tpnt1->symbol_scope) tpnt1->symbol_scope = rpnt;
265 * OK, now attach the entire chain at the end
267 rpnt->next = _dl_symbol_tables;
271 * Relocation of the GOT entries for MIPS have to be done
272 * after all the libraries have been loaded.
274 _dl_perform_mips_global_got_relocations(tpnt);
277 #ifdef __SUPPORT_LD_DEBUG__
279 _dl_dprintf(_dl_debug_file, "Beginning dlopen relocation fixups\n");
282 * OK, now all of the kids are tucked into bed in their proper addresses.
283 * Now we go through and look for REL and RELA records that indicate fixups
284 * to the GOT tables. We need to do this in reverse order so that COPY
285 * directives work correctly */
286 if (_dl_fixup(dyn_chain, dyn_chain->flags))
289 /* TODO: Should we set the protections of all pages back to R/O now ? */
292 /* Notify the debugger we have added some objects. */
293 if (_dl_debug_addr) {
294 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
295 if (dl_brk != NULL) {
296 _dl_debug_addr->r_state = RT_ADD;
299 _dl_debug_addr->r_state = RT_CONSISTENT;
304 #if 0 //def __SUPPORT_LD_DEBUG__
309 #if defined (__LIBDL_SHARED__)
310 /* Find the last library so we can run things in the right order */
311 for (tpnt = dyn_chain->dyn; tpnt->next!=NULL; tpnt = tpnt->next)
314 /* Run the ctors and set up the dtors */
315 for (; tpnt != dyn_chain->dyn->prev; tpnt=tpnt->prev)
317 /* Apparently crt1 for the application is responsible for handling this.
318 * We only need to run the init/fini for shared libraries
320 if (tpnt->libtype == program_interpreter)
322 if (tpnt->libtype == elf_executable)
324 if (tpnt->init_flag & INIT_FUNCS_CALLED)
326 tpnt->init_flag |= INIT_FUNCS_CALLED;
328 if (tpnt->dynamic_info[DT_INIT]) {
329 void (*dl_elf_func) (void);
330 dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
331 if (dl_elf_func && *dl_elf_func != NULL) {
332 #ifdef __SUPPORT_LD_DEBUG__
334 _dl_dprintf(2, "running ctors for library %s at '%x'\n", tpnt->libname, dl_elf_func);
339 if (tpnt->dynamic_info[DT_FINI]) {
340 void (*dl_elf_func) (void);
341 dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
342 if (dl_elf_func && *dl_elf_func != NULL) {
343 #ifdef __SUPPORT_LD_DEBUG__
345 _dl_dprintf(2, "setting up dtors for library %s at '%x'\n", tpnt->libname, dl_elf_func);
352 return (void *) dyn_chain;
355 /* Something went wrong. Clean up and return NULL. */
357 do_dlclose(dyn_chain, 0);
360 weak_alias(_dlopen, dlopen);
362 void *_dlsym(void *vhandle, const char *name)
364 struct elf_resolve *tpnt, *tfrom;
365 struct dyn_elf *handle;
367 struct dyn_elf *rpnt;
370 handle = (struct dyn_elf *) vhandle;
372 /* First of all verify that we have a real handle
373 of some kind. Return NULL if not a valid handle. */
376 handle = _dl_symbol_tables;
377 else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
378 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
382 _dl_error_number = LD_BAD_HANDLE;
385 } else if (handle == RTLD_NEXT) {
387 * Try and locate the module we were called from - we
388 * need this so that we know where to start searching
389 * from. We never pass RTLD_NEXT down into the actual
390 * dynamic loader itself, as it doesn't know
391 * how to properly treat it.
393 from = (ElfW(Addr)) __builtin_return_address(0);
396 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
398 if (tpnt->loadaddr < from
399 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
406 ret = _dl_find_hash((char*)name, handle, 0);
412 _dl_error_number = LD_NO_SYMBOL;
415 weak_alias(_dlsym, dlsym);
417 static int do_dlclose(void *vhandle, int need_fini)
419 struct dyn_elf *rpnt, *rpnt1;
420 struct dyn_elf *spnt, *spnt1;
422 struct elf_resolve *tpnt;
423 int (*dl_elf_fini) (void);
424 void (*dl_brk) (void);
425 struct dyn_elf *handle;
429 handle = (struct dyn_elf *) vhandle;
431 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
432 if (rpnt == handle) {
439 _dl_error_number = LD_BAD_HANDLE;
443 /* OK, this is a valid handle - now close out the file.
444 * We check if we need to call fini () on the handle. */
445 spnt = need_fini ? handle : handle->next;
446 for (; spnt; spnt = spnt1) {
449 /* We appended the module list to the end - when we get back here,
450 quit. The access counts were not adjusted to account for being here. */
451 if (spnt == _dl_symbol_tables)
453 if (spnt->dyn->usage_count == 1
454 && spnt->dyn->libtype == loaded_file) {
456 /* Apparently crt1 for the application is responsible for handling this.
457 * We only need to run the init/fini for shared libraries
460 if (tpnt->dynamic_info[DT_FINI]) {
461 dl_elf_fini = (int (*)(void)) (tpnt->loadaddr +
462 tpnt->dynamic_info[DT_FINI]);
468 rpnt1->next_handle = rpnt->next_handle;
470 _dl_handles = rpnt->next_handle;
472 /* OK, this is a valid handle - now close out the file */
473 for (rpnt = handle; rpnt; rpnt = rpnt1) {
476 /* We appended the module list to the end - when we get back here,
477 quit. The access counts were not adjusted to account for being here. */
478 if (rpnt == _dl_symbol_tables)
481 rpnt->dyn->usage_count--;
482 if (rpnt->dyn->usage_count == 0
483 && rpnt->dyn->libtype == loaded_file) {
485 /* Apparently crt1 for the application is responsible for handling this.
486 * We only need to run the init/fini for shared libraries
490 /* We have to do this above, before we start closing objects.
491 * Otherwise when the needed symbols for _fini handling are
492 * resolved a coredump would occur. Rob Ryan (robr@cmu.edu)*/
493 if (tpnt->dynamic_info[DT_FINI]) {
494 dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
499 for (i = 0, ppnt = rpnt->dyn->ppnt;
500 i < rpnt->dyn->n_phent; ppnt++, i++) {
501 if (ppnt->p_type != PT_LOAD)
503 if (end < ppnt->p_vaddr + ppnt->p_memsz)
504 end = ppnt->p_vaddr + ppnt->p_memsz;
506 _dl_munmap((void*)rpnt->dyn->loadaddr, end);
507 /* Next, remove rpnt->dyn from the loaded_module list */
508 if (_dl_loaded_modules == rpnt->dyn) {
509 _dl_loaded_modules = rpnt->dyn->next;
510 if (_dl_loaded_modules)
511 _dl_loaded_modules->prev = 0;
513 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
514 if (tpnt->next == rpnt->dyn) {
515 tpnt->next = tpnt->next->next;
517 tpnt->next->prev = tpnt;
520 free(rpnt->dyn->libname);
527 if (_dl_debug_addr) {
528 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
529 if (dl_brk != NULL) {
530 _dl_debug_addr->r_state = RT_DELETE;
533 _dl_debug_addr->r_state = RT_CONSISTENT;
541 int _dlclose(void *vhandle)
543 return do_dlclose(vhandle, 1);
545 weak_alias(_dlclose, dlclose);
547 const char *_dlerror(void)
551 if (!_dl_error_number)
553 retval = dl_error_names[_dl_error_number];
554 _dl_error_number = 0;
557 weak_alias(_dlerror, dlerror);
560 * Dump information to stderrr about the current loaded modules
562 static char *type[] = { "Lib", "Exe", "Int", "Mod" };
566 struct elf_resolve *tpnt;
567 struct dyn_elf *rpnt, *hpnt;
569 _dl_dprintf(2, "List of loaded modules\n");
570 /* First start with a complete list of all of the loaded files. */
571 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
572 _dl_dprintf(2, "\t%x %x %x %s %d %s\n",
573 (unsigned) tpnt->loadaddr, (unsigned) tpnt,
574 (unsigned) tpnt->symbol_scope,
576 tpnt->usage_count, tpnt->libname);
579 /* Next dump the module list for the application itself */
580 _dl_dprintf(2, "\nModules for application (%x):\n",
581 (unsigned) _dl_symbol_tables);
582 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
583 _dl_dprintf(2, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname);
585 for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
586 _dl_dprintf(2, "Modules for handle %x\n", (unsigned) hpnt);
587 for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
588 _dl_dprintf(2, "\t%x %s\n", (unsigned) rpnt->dyn,
592 weak_alias(_dlinfo, dlinfo);
594 int _dladdr(void *__address, Dl_info * __dlip)
596 struct elf_resolve *pelf;
597 struct elf_resolve *rpnt;
602 * Try and locate the module address is in
607 _dl_dprintf(2, "dladdr( %x, %x )\n", __address, __dlip);
610 for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
611 struct elf_resolve *tpnt;
615 _dl_dprintf(2, "Module \"%s\" at %x\n",
616 tpnt->libname, tpnt->loadaddr);
618 if (tpnt->loadaddr < (ElfW(Addr)) __address
619 && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
629 * Try and locate the symbol of address
641 symtab = (Elf32_Sym *) (pelf->dynamic_info[DT_SYMTAB] + pelf->loadaddr);
642 strtab = (char *) (pelf->dynamic_info[DT_STRTAB] + pelf->loadaddr);
645 for (hn = 0; hn < pelf->nbucket; hn++) {
646 for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
647 ElfW(Addr) symbol_addr;
649 symbol_addr = pelf->loadaddr + symtab[si].st_value;
650 if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
656 _dl_dprintf(2, "Symbol \"%s\" at %x\n",
657 strtab + symtab[si].st_name, symbol_addr);
663 __dlip->dli_fname = pelf->libname;
664 __dlip->dli_fbase = (void *)pelf->loadaddr;
665 __dlip->dli_sname = strtab + symtab[sn].st_name;
666 __dlip->dli_saddr = (void *)sa;
671 weak_alias(_dladdr, dladdr);