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 void __attribute__ ((destructor)) dl_cleanup(void)
123 for (d = _dl_handles; d; d = d->next_handle) {
128 void *dlopen(const char *libname, int flag)
130 struct elf_resolve *tpnt, *tfrom, *tcurr;
131 struct dyn_elf *dyn_chain, *rpnt = NULL, *dyn_ptr;
132 struct dyn_elf *dpnt;
133 static int dl_init = 0;
135 struct elf_resolve *tpnt1;
136 void (*dl_brk) (void);
139 /* A bit of sanity checking... */
140 if (!(flag & (RTLD_LAZY|RTLD_NOW))) {
141 _dl_error_number = LD_BAD_HANDLE;
145 from = (ElfW(Addr)) __builtin_return_address(0);
147 /* Have the dynamic linker use the regular malloc function now */
150 #if defined (__LIBDL_SHARED__)
151 _dl_malloc_function = malloc;
155 /* Cover the trivial case first */
157 return _dl_symbol_tables;
162 * Try and locate the module we were called from - we
163 * need this so that we get the correct RPATH. Note that
164 * this is the current behavior under Solaris, but the
165 * ABI+ specifies that we should only use the RPATH from
166 * the application. Thus this may go away at some time
170 for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
172 if (tpnt->loadaddr < from
173 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr))
176 for(rpnt = _dl_symbol_tables; rpnt->next; rpnt=rpnt->next);
177 /* Try to load the specified library */
178 #ifdef __SUPPORT_LD_DEBUG__
180 fprintf(stderr, "Trying to dlopen '%s'\n", (char*)libname);
182 tpnt = _dl_check_if_named_library_is_loaded((char *)libname, 0);
184 tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname, 0);
192 dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
193 _dl_memset(dyn_chain, 0, sizeof(struct dyn_elf));
194 dyn_chain->dyn = tpnt;
195 tpnt->rtld_flags |= RTLD_GLOBAL;
197 dyn_chain->next_handle = _dl_handles;
198 _dl_handles = dyn_ptr = dyn_chain;
200 #ifdef __SUPPORT_LD_DEBUG__
202 fprintf(stderr, "Looking for needed libraries\n");
205 for (tcurr = tpnt; tcurr; tcurr = tcurr->next)
209 for (dpnt = (Elf32_Dyn *) tcurr->dynamic_addr; dpnt->d_tag; dpnt++) {
210 if (dpnt->d_tag == DT_NEEDED) {
213 lpntstr = (char*) (tcurr->loadaddr + tcurr->dynamic_info[DT_STRTAB] +
215 name = _dl_get_last_path_component(lpntstr);
216 tpnt1 = _dl_check_if_named_library_is_loaded(name, 0);
217 #ifdef __SUPPORT_LD_DEBUG__
219 fprintf(stderr, "Trying to load '%s', needed by '%s'\n",
220 lpntstr, tcurr->libname);
222 dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
223 _dl_memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
224 dyn_ptr = dyn_ptr->next;
225 dyn_ptr->dyn = tpnt1;
226 tpnt->rtld_flags |= RTLD_GLOBAL;
228 tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpntstr, 0);
231 dyn_ptr->dyn = tpnt1;
233 tpnt1->usage_count++;
239 if (dyn_chain->dyn->init_flag & INIT_FUNCS_CALLED) {
240 /* If the init and fini stuff has already been run, that means
241 * the dlopen'd library has already been loaded, and nothing
242 * further needs to be done. */
243 return (void *) dyn_chain;
247 * Relocation of the GOT entries for MIPS have to be done
248 * after all the libraries have been loaded.
250 _dl_perform_mips_global_got_relocations(tpnt);
253 #ifdef __SUPPORT_LD_DEBUG__
255 fprintf(stderr, "Beginning dlopen relocation fixups\n");
258 * OK, now all of the kids are tucked into bed in their proper addresses.
259 * Now we go through and look for REL and RELA records that indicate fixups
260 * to the GOT tables. We need to do this in reverse order so that COPY
261 * directives work correctly */
262 now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
263 if (getenv("LD_BIND_NOW"))
265 if (_dl_fixup(dyn_chain, now_flag))
268 /* TODO: Should we set the protections of all pages back to R/O now ? */
271 /* Notify the debugger we have added some objects. */
272 if (_dl_debug_addr) {
273 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
274 if (dl_brk != NULL) {
275 _dl_debug_addr->r_state = RT_ADD;
278 _dl_debug_addr->r_state = RT_CONSISTENT;
283 #if defined (__LIBDL_SHARED__)
284 /* Find the last library so we can run things in the right order */
285 for (tpnt = dyn_chain->dyn; tpnt->next!=NULL; tpnt = tpnt->next)
288 /* Run the ctors and set up the dtors */
289 for (; tpnt != dyn_chain->dyn->prev; tpnt=tpnt->prev)
291 /* Apparently crt1 for the application is responsible for handling this.
292 * We only need to run the init/fini for shared libraries
294 if (tpnt->libtype == program_interpreter)
296 if (tpnt->libtype == elf_executable)
298 if (tpnt->init_flag & INIT_FUNCS_CALLED)
300 tpnt->init_flag |= INIT_FUNCS_CALLED;
302 if (tpnt->dynamic_info[DT_INIT]) {
303 void (*dl_elf_func) (void);
304 dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
305 if (dl_elf_func && *dl_elf_func != NULL) {
306 #ifdef __SUPPORT_LD_DEBUG__
308 fprintf(stderr, "running ctors for library %s at '%x'\n", tpnt->libname, dl_elf_func);
315 return (void *) dyn_chain;
318 /* Something went wrong. Clean up and return NULL. */
320 do_dlclose(dyn_chain, 0);
324 void *dlsym(void *vhandle, const char *name)
326 struct elf_resolve *tpnt, *tfrom;
327 struct dyn_elf *handle;
329 struct dyn_elf *rpnt;
332 handle = (struct dyn_elf *) vhandle;
334 /* First of all verify that we have a real handle
335 of some kind. Return NULL if not a valid handle. */
338 handle = _dl_symbol_tables;
339 else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
340 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
344 _dl_error_number = LD_BAD_HANDLE;
347 } else if (handle == RTLD_NEXT) {
349 * Try and locate the module we were called from - we
350 * need this so that we know where to start searching
351 * from. We never pass RTLD_NEXT down into the actual
352 * dynamic loader itself, as it doesn't know
353 * how to properly treat it.
355 from = (ElfW(Addr)) __builtin_return_address(0);
358 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
360 if (tpnt->loadaddr < from
361 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
368 ret = _dl_find_hash((char*)name, handle, 0);
374 _dl_error_number = LD_NO_SYMBOL;
378 static int do_dlclose(void *vhandle, int need_fini)
380 struct dyn_elf *rpnt, *rpnt1;
382 struct elf_resolve *tpnt;
383 int (*dl_elf_fini) (void);
384 void (*dl_brk) (void);
385 struct dyn_elf *handle;
389 handle = (struct dyn_elf *) vhandle;
391 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
398 _dl_error_number = LD_BAD_HANDLE;
402 rpnt1->next_handle = rpnt->next_handle;
404 _dl_handles = rpnt->next_handle;
405 /* OK, this is a valid handle - now close out the file */
406 for (rpnt = handle; rpnt; rpnt = rpnt->next) {
408 if (--tpnt->usage_count == 0) {
409 if (need_fini && tpnt->dynamic_info[DT_FINI]) {
410 dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
415 for (i = 0, ppnt = tpnt->ppnt;
416 i < tpnt->n_phent; ppnt++, i++) {
417 if (ppnt->p_type != PT_LOAD)
419 if (end < ppnt->p_vaddr + ppnt->p_memsz)
420 end = ppnt->p_vaddr + ppnt->p_memsz;
422 _dl_munmap((void*)tpnt->loadaddr, end);
423 /* Next, remove tpnt from the loaded_module list */
424 if (_dl_loaded_modules == tpnt) {
425 _dl_loaded_modules = tpnt->next;
426 if (_dl_loaded_modules)
427 _dl_loaded_modules->prev = 0;
429 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
430 if (tpnt->next == rpnt->dyn) {
431 tpnt->next = tpnt->next->next;
433 tpnt->next->prev = tpnt;
437 /* Next, remove tpnt from the global symbol table list */
438 if (_dl_symbol_tables->dyn == rpnt->dyn) {
439 _dl_symbol_tables = rpnt->next;
440 if (_dl_symbol_tables)
441 _dl_symbol_tables->prev = 0;
443 for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
444 if (rpnt1->next->dyn == rpnt->dyn) {
446 rpnt1->next = rpnt1->next->next;
448 rpnt1->next->prev = rpnt1;
452 free(rpnt->dyn->libname);
459 if (_dl_debug_addr) {
460 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
461 if (dl_brk != NULL) {
462 _dl_debug_addr->r_state = RT_DELETE;
465 _dl_debug_addr->r_state = RT_CONSISTENT;
473 int dlclose(void *vhandle)
475 return do_dlclose(vhandle, 1);
478 const char *dlerror(void)
482 if (!_dl_error_number)
484 retval = dl_error_names[_dl_error_number];
485 _dl_error_number = 0;
490 * Dump information to stderrr about the current loaded modules
492 static char *type[] = { "Lib", "Exe", "Int", "Mod" };
496 struct elf_resolve *tpnt;
497 struct dyn_elf *rpnt, *hpnt;
499 fprintf(stderr, "List of loaded modules\n");
500 /* First start with a complete list of all of the loaded files. */
501 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
502 fprintf(stderr, "\t%x %x %x %s %d %s\n",
503 (unsigned) tpnt->loadaddr, (unsigned) tpnt,
504 (unsigned) tpnt->symbol_scope,
506 tpnt->usage_count, tpnt->libname);
509 /* Next dump the module list for the application itself */
510 fprintf(stderr, "\nModules for application (%x):\n",
511 (unsigned) _dl_symbol_tables);
512 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
513 fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname);
515 for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
516 fprintf(stderr, "Modules for handle %x\n", (unsigned) hpnt);
517 for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
518 fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn,
523 int dladdr(void *__address, Dl_info * __dlip)
525 struct elf_resolve *pelf;
526 struct elf_resolve *rpnt;
531 * Try and locate the module address is in
536 fprintf(stderr, "dladdr( %x, %x )\n", __address, __dlip);
539 for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
540 struct elf_resolve *tpnt;
544 fprintf(stderr, "Module \"%s\" at %x\n",
545 tpnt->libname, tpnt->loadaddr);
547 if (tpnt->loadaddr < (ElfW(Addr)) __address
548 && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
558 * Try and locate the symbol of address
570 symtab = (Elf32_Sym *) (pelf->dynamic_info[DT_SYMTAB] + pelf->loadaddr);
571 strtab = (char *) (pelf->dynamic_info[DT_STRTAB] + pelf->loadaddr);
574 for (hn = 0; hn < pelf->nbucket; hn++) {
575 for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
576 ElfW(Addr) symbol_addr;
578 symbol_addr = pelf->loadaddr + symtab[si].st_value;
579 if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
585 fprintf(stderr, "Symbol \"%s\" at %x\n",
586 strtab + symtab[si].st_name, symbol_addr);
592 __dlip->dli_fname = pelf->libname;
593 __dlip->dli_fbase = (void *)pelf->loadaddr;
594 __dlip->dli_sname = strtab + symtab[sn].st_name;
595 __dlip->dli_saddr = (void *)sa;