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__));
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__));
67 extern char *_dl_debug_symbols __attribute__ ((__weak__));
68 extern char *_dl_debug_move __attribute__ ((__weak__));
69 extern char *_dl_debug_reloc __attribute__ ((__weak__));
70 extern char *_dl_debug_detail __attribute__ ((__weak__));
71 extern char *_dl_debug_nofixups __attribute__ ((__weak__));
72 extern char *_dl_debug_bindings __attribute__ ((__weak__));
73 extern int _dl_debug_file __attribute__ ((__weak__));
77 #else /* __LIBDL_SHARED__ */
79 /* When libdl is linked as a static library, we need to replace all
80 * the symbols that otherwise would have been loaded in from ldso... */
82 #ifdef __SUPPORT_LD_DEBUG__
84 char *_dl_debug_symbols = 0;
85 char *_dl_debug_move = 0;
86 char *_dl_debug_reloc = 0;
87 char *_dl_debug_detail = 0;
88 char *_dl_debug_nofixups = 0;
89 char *_dl_debug_bindings = 0;
90 int _dl_debug_file = 2;
92 char *_dl_library_path = 0;
93 char *_dl_ldsopath = 0;
94 struct r_debug *_dl_debug_addr = NULL;
95 static unsigned char *_dl_malloc_addr, *_dl_mmap_zero;
96 void *(*_dl_malloc_function) (size_t size);
98 int _dl_fixup(struct dyn_elf *rpnt, int lazy);
99 #include "../ldso/dl-progname.h" /* Pull in the name of ld.so */
100 #include "../ldso/dl-hash.c"
101 #define _dl_trace_loaded_objects 0
102 #include "../ldso/dl-elf.c"
105 static int do_dlclose(void *, int need_fini);
108 static const char *dl_error_names[] = {
111 "Unable to open /dev/zero",
113 #if defined (__i386__)
115 #elif defined (__sparc__)
117 #elif defined (__mc68000__)
120 "Unrecognized binary type",
122 "Not an ELF shared library",
123 "Unable to mmap file",
124 "No dynamic section",
125 #ifdef ELF_USES_RELOCA
126 "Unable to process REL relocs",
128 "Unable to process RELA relocs",
131 "Unable to resolve symbol"
134 static void __attribute__ ((destructor)) dl_cleanup(void)
138 for (d = _dl_handles; d; d = d->next_handle)
139 if (d->dyn->libtype == loaded_file && d->dyn->dynamic_info[DT_FINI]) {
140 (* ((int (*)(void)) (d->dyn->loadaddr + d->dyn->dynamic_info[DT_FINI]))) ();
141 d->dyn->dynamic_info[DT_FINI] = 0;
145 void *_dlopen(const char *libname, int flag)
147 struct elf_resolve *tpnt, *tfrom, *tcurr;
148 struct dyn_elf *dyn_chain, *rpnt = NULL;
149 struct dyn_elf *dpnt;
150 static int dl_init = 0;
152 struct elf_resolve *tpnt1;
153 void (*dl_brk) (void);
155 /* A bit of sanity checking... */
156 if (!(flag & (RTLD_LAZY|RTLD_NOW))) {
157 _dl_error_number = LD_BAD_HANDLE;
161 from = (ElfW(Addr)) __builtin_return_address(0);
163 /* Have the dynamic linker use the regular malloc function now */
166 _dl_malloc_function = malloc;
169 /* Cover the trivial case first */
171 return _dl_symbol_tables;
176 * Try and locate the module we were called from - we
177 * need this so that we get the correct RPATH. Note that
178 * this is the current behavior under Solaris, but the
179 * ABI+ specifies that we should only use the RPATH from
180 * the application. Thus this may go away at some time
184 for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
186 if (tpnt->loadaddr < from
187 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr))
191 /* Try to load the specified library */
192 #ifdef __SUPPORT_LD_DEBUG__
194 _dl_dprintf(_dl_debug_file, "Trying to dlopen '%s'\n", (char*)libname);
196 if (!(tpnt = _dl_check_if_named_library_is_loaded((char *)libname, 0)))
197 tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname, 0);
203 dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
204 _dl_memset(dyn_chain, 0, sizeof(struct dyn_elf));
205 dyn_chain->dyn = tpnt;
206 dyn_chain->flags = flag;
207 if (!tpnt->symbol_scope)
208 tpnt->symbol_scope = dyn_chain;
210 dyn_chain->next_handle = _dl_handles;
211 _dl_handles = rpnt = dyn_chain;
213 if (tpnt->init_flag & INIT_FUNCS_CALLED) {
214 /* If the init and fini stuff has already been run, that means
215 * the dlopen'd library has already been loaded, and nothing
216 * further needs to be done. */
217 return (void *) dyn_chain;
221 #ifdef __SUPPORT_LD_DEBUG__
223 _dl_dprintf(_dl_debug_file, "Looking for needed libraries\n");
226 for (tcurr = tpnt; tcurr; tcurr = tcurr->next)
230 for (dpnt = (Elf32_Dyn *) tcurr->dynamic_addr; dpnt->d_tag; dpnt++) {
231 if (dpnt->d_tag == DT_NEEDED) {
234 lpntstr = (char*) (tcurr->loadaddr + tcurr->dynamic_info[DT_STRTAB] +
236 name = _dl_get_last_path_component(lpntstr);
238 if ((tpnt1 = _dl_check_if_named_library_is_loaded(name, 0)))
241 #ifdef __SUPPORT_LD_DEBUG__
243 _dl_dprintf(_dl_debug_file, "Trying to load '%s', needed by '%s'\n",
244 lpntstr, tcurr->libname);
247 if (!(tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpntstr, 0))) {
251 rpnt->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
252 _dl_memset (rpnt->next, 0, sizeof (struct dyn_elf));
254 if (!tpnt1->symbol_scope) tpnt1->symbol_scope = rpnt;
262 * OK, now attach the entire chain at the end
264 rpnt->next = _dl_symbol_tables;
268 * Relocation of the GOT entries for MIPS have to be done
269 * after all the libraries have been loaded.
271 _dl_perform_mips_global_got_relocations(tpnt);
274 #ifdef __SUPPORT_LD_DEBUG__
276 _dl_dprintf(_dl_debug_file, "Beginning dlopen relocation fixups\n");
279 * OK, now all of the kids are tucked into bed in their proper addresses.
280 * Now we go through and look for REL and RELA records that indicate fixups
281 * to the GOT tables. We need to do this in reverse order so that COPY
282 * directives work correctly */
283 if (_dl_fixup(dyn_chain, dyn_chain->flags))
286 /* TODO: Should we set the protections of all pages back to R/O now ? */
289 /* Notify the debugger we have added some objects. */
290 if (_dl_debug_addr) {
291 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
292 if (dl_brk != NULL) {
293 _dl_debug_addr->r_state = RT_ADD;
296 _dl_debug_addr->r_state = RT_CONSISTENT;
301 #if 0 //def __SUPPORT_LD_DEBUG__
306 #if defined (__LIBDL_SHARED__)
307 /* Find the last library so we can run things in the right order */
308 for (tpnt = dyn_chain->dyn; tpnt->next!=NULL; tpnt = tpnt->next)
311 /* Run the ctors and set up the dtors */
312 for (; tpnt != dyn_chain->dyn->prev; tpnt=tpnt->prev)
314 /* Apparently crt1 for the application is responsible for handling this.
315 * We only need to run the init/fini for shared libraries
317 if (tpnt->libtype == program_interpreter)
319 if (tpnt->libtype == elf_executable)
321 if (tpnt->init_flag & INIT_FUNCS_CALLED)
323 tpnt->init_flag |= INIT_FUNCS_CALLED;
325 if (tpnt->dynamic_info[DT_INIT]) {
326 void (*dl_elf_func) (void);
327 dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
328 if (dl_elf_func && *dl_elf_func != NULL) {
329 #ifdef __SUPPORT_LD_DEBUG__
331 _dl_dprintf(2, "running ctors for library %s at '%x'\n", tpnt->libname, dl_elf_func);
336 if (tpnt->dynamic_info[DT_FINI]) {
337 void (*dl_elf_func) (void);
338 dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
339 if (dl_elf_func && *dl_elf_func != NULL) {
340 #ifdef __SUPPORT_LD_DEBUG__
342 _dl_dprintf(2, "setting up dtors for library %s at '%x'\n", tpnt->libname, dl_elf_func);
349 return (void *) dyn_chain;
352 /* Something went wrong. Clean up and return NULL. */
354 do_dlclose(dyn_chain, 0);
357 weak_alias(_dlopen, dlopen);
359 void *_dlsym(void *vhandle, const char *name)
361 struct elf_resolve *tpnt, *tfrom;
362 struct dyn_elf *handle;
364 struct dyn_elf *rpnt;
367 handle = (struct dyn_elf *) vhandle;
369 /* First of all verify that we have a real handle
370 of some kind. Return NULL if not a valid handle. */
373 handle = _dl_symbol_tables;
374 else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
375 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
379 _dl_error_number = LD_BAD_HANDLE;
382 } else if (handle == RTLD_NEXT) {
384 * Try and locate the module we were called from - we
385 * need this so that we know where to start searching
386 * from. We never pass RTLD_NEXT down into the actual
387 * dynamic loader itself, as it doesn't know
388 * how to properly treat it.
390 from = (ElfW(Addr)) __builtin_return_address(0);
393 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
395 if (tpnt->loadaddr < from
396 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
403 ret = _dl_find_hash((char*)name, handle, 0);
409 _dl_error_number = LD_NO_SYMBOL;
412 weak_alias(_dlsym, dlsym);
414 static int do_dlclose(void *vhandle, int need_fini)
416 struct dyn_elf *rpnt, *rpnt1;
417 struct dyn_elf *spnt, *spnt1;
419 struct elf_resolve *tpnt;
420 int (*dl_elf_fini) (void);
421 void (*dl_brk) (void);
422 struct dyn_elf *handle;
426 handle = (struct dyn_elf *) vhandle;
428 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
429 if (rpnt == handle) {
436 _dl_error_number = LD_BAD_HANDLE;
440 /* OK, this is a valid handle - now close out the file.
441 * We check if we need to call fini () on the handle. */
442 spnt = need_fini ? handle : handle->next;
443 for (; spnt; spnt = spnt1) {
446 /* We appended the module list to the end - when we get back here,
447 quit. The access counts were not adjusted to account for being here. */
448 if (spnt == _dl_symbol_tables)
450 if (spnt->dyn->usage_count == 1
451 && spnt->dyn->libtype == loaded_file) {
453 /* Apparently crt1 for the application is responsible for handling this.
454 * We only need to run the init/fini for shared libraries
457 if (tpnt->dynamic_info[DT_FINI]) {
458 dl_elf_fini = (int (*)(void)) (tpnt->loadaddr +
459 tpnt->dynamic_info[DT_FINI]);
465 rpnt1->next_handle = rpnt->next_handle;
467 _dl_handles = rpnt->next_handle;
469 /* OK, this is a valid handle - now close out the file */
470 for (rpnt = handle; rpnt; rpnt = rpnt1) {
473 /* We appended the module list to the end - when we get back here,
474 quit. The access counts were not adjusted to account for being here. */
475 if (rpnt == _dl_symbol_tables)
478 rpnt->dyn->usage_count--;
479 if (rpnt->dyn->usage_count == 0
480 && rpnt->dyn->libtype == loaded_file) {
482 /* Apparently crt1 for the application is responsible for handling this.
483 * We only need to run the init/fini for shared libraries
487 /* We have to do this above, before we start closing objects.
488 * Otherwise when the needed symbols for _fini handling are
489 * resolved a coredump would occur. Rob Ryan (robr@cmu.edu)*/
490 if (tpnt->dynamic_info[DT_FINI]) {
491 dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
496 for (i = 0, ppnt = rpnt->dyn->ppnt;
497 i < rpnt->dyn->n_phent; ppnt++, i++) {
498 if (ppnt->p_type != PT_LOAD)
500 if (end < ppnt->p_vaddr + ppnt->p_memsz)
501 end = ppnt->p_vaddr + ppnt->p_memsz;
503 _dl_munmap((void*)rpnt->dyn->loadaddr, end);
504 /* Next, remove rpnt->dyn from the loaded_module list */
505 if (_dl_loaded_modules == rpnt->dyn) {
506 _dl_loaded_modules = rpnt->dyn->next;
507 if (_dl_loaded_modules)
508 _dl_loaded_modules->prev = 0;
510 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
511 if (tpnt->next == rpnt->dyn) {
512 tpnt->next = tpnt->next->next;
514 tpnt->next->prev = tpnt;
517 free(rpnt->dyn->libname);
524 if (_dl_debug_addr) {
525 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
526 if (dl_brk != NULL) {
527 _dl_debug_addr->r_state = RT_DELETE;
530 _dl_debug_addr->r_state = RT_CONSISTENT;
538 int _dlclose(void *vhandle)
540 return do_dlclose(vhandle, 1);
542 weak_alias(_dlclose, dlclose);
544 const char *_dlerror(void)
548 if (!_dl_error_number)
550 retval = dl_error_names[_dl_error_number];
551 _dl_error_number = 0;
554 weak_alias(_dlerror, dlerror);
557 * Dump information to stderrr about the current loaded modules
559 static char *type[] = { "Lib", "Exe", "Int", "Mod" };
563 struct elf_resolve *tpnt;
564 struct dyn_elf *rpnt, *hpnt;
566 _dl_dprintf(2, "List of loaded modules\n");
567 /* First start with a complete list of all of the loaded files. */
568 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
569 _dl_dprintf(2, "\t%x %x %x %s %d %s\n",
570 (unsigned) tpnt->loadaddr, (unsigned) tpnt,
571 (unsigned) tpnt->symbol_scope,
573 tpnt->usage_count, tpnt->libname);
576 /* Next dump the module list for the application itself */
577 _dl_dprintf(2, "\nModules for application (%x):\n",
578 (unsigned) _dl_symbol_tables);
579 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
580 _dl_dprintf(2, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname);
582 for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
583 _dl_dprintf(2, "Modules for handle %x\n", (unsigned) hpnt);
584 for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
585 _dl_dprintf(2, "\t%x %s\n", (unsigned) rpnt->dyn,
589 weak_alias(_dlinfo, dlinfo);
591 int _dladdr(void *__address, Dl_info * __dlip)
593 struct elf_resolve *pelf;
594 struct elf_resolve *rpnt;
599 * Try and locate the module address is in
604 _dl_dprintf(2, "dladdr( %x, %x )\n", __address, __dlip);
607 for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
608 struct elf_resolve *tpnt;
612 _dl_dprintf(2, "Module \"%s\" at %x\n",
613 tpnt->libname, tpnt->loadaddr);
615 if (tpnt->loadaddr < (ElfW(Addr)) __address
616 && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
626 * Try and locate the symbol of address
638 symtab = (Elf32_Sym *) (pelf->dynamic_info[DT_SYMTAB] + pelf->loadaddr);
639 strtab = (char *) (pelf->dynamic_info[DT_STRTAB] + pelf->loadaddr);
642 for (hn = 0; hn < pelf->nbucket; hn++) {
643 for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
644 ElfW(Addr) symbol_addr;
646 symbol_addr = pelf->loadaddr + symtab[si].st_value;
647 if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
653 _dl_dprintf(2, "Symbol \"%s\" at %x\n",
654 strtab + symtab[si].st_name, symbol_addr);
660 __dlip->dli_fname = pelf->libname;
661 __dlip->dli_fbase = (void *)pelf->loadaddr;
662 __dlip->dli_sname = strtab + symtab[sn].st_name;
663 __dlip->dli_saddr = (void *)sa;
668 weak_alias(_dladdr, dladdr);