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 if (!(tpnt = _dl_check_if_named_library_is_loaded((char *)libname, 0)))
183 tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname, 0);
189 dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
190 _dl_memset(dyn_chain, 0, sizeof(struct dyn_elf));
191 dyn_chain->dyn = tpnt;
192 tpnt->rtld_flags |= RTLD_GLOBAL;
194 dyn_chain->next_handle = _dl_handles;
195 _dl_handles = dyn_ptr = dyn_chain;
197 #ifdef __SUPPORT_LD_DEBUG__
199 fprintf(stderr, "Looking for needed libraries\n");
202 for (tcurr = tpnt; tcurr; tcurr = tcurr->next)
206 for (dpnt = (Elf32_Dyn *) tcurr->dynamic_addr; dpnt->d_tag; dpnt++) {
207 if (dpnt->d_tag == DT_NEEDED) {
210 lpntstr = (char*) (tcurr->loadaddr + tcurr->dynamic_info[DT_STRTAB] +
212 name = _dl_get_last_path_component(lpntstr);
213 tpnt1 = _dl_check_if_named_library_is_loaded(name, 0);
214 #ifdef __SUPPORT_LD_DEBUG__
216 fprintf(stderr, "Trying to load '%s', needed by '%s'\n",
217 lpntstr, tcurr->libname);
219 dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
220 _dl_memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
221 dyn_ptr = dyn_ptr->next;
222 dyn_ptr->dyn = tpnt1;
223 tpnt->rtld_flags |= RTLD_GLOBAL;
225 tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpntstr, 0);
228 dyn_ptr->dyn = tpnt1;
230 tpnt1->usage_count++;
236 if (dyn_chain->dyn->init_flag & INIT_FUNCS_CALLED) {
237 /* If the init and fini stuff has already been run, that means
238 * the dlopen'd library has already been loaded, and nothing
239 * further needs to be done. */
240 return (void *) dyn_chain;
244 * Relocation of the GOT entries for MIPS have to be done
245 * after all the libraries have been loaded.
247 _dl_perform_mips_global_got_relocations(tpnt);
250 #ifdef __SUPPORT_LD_DEBUG__
252 fprintf(stderr, "Beginning dlopen relocation fixups\n");
255 * OK, now all of the kids are tucked into bed in their proper addresses.
256 * Now we go through and look for REL and RELA records that indicate fixups
257 * to the GOT tables. We need to do this in reverse order so that COPY
258 * directives work correctly */
259 now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
260 if (getenv("LD_BIND_NOW"))
262 if (_dl_fixup(dyn_chain, now_flag))
265 /* TODO: Should we set the protections of all pages back to R/O now ? */
268 /* Notify the debugger we have added some objects. */
269 if (_dl_debug_addr) {
270 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
271 if (dl_brk != NULL) {
272 _dl_debug_addr->r_state = RT_ADD;
275 _dl_debug_addr->r_state = RT_CONSISTENT;
280 #if defined (__LIBDL_SHARED__)
281 /* Find the last library so we can run things in the right order */
282 for (tpnt = dyn_chain->dyn; tpnt->next!=NULL; tpnt = tpnt->next)
285 /* Run the ctors and set up the dtors */
286 for (; tpnt != dyn_chain->dyn->prev; tpnt=tpnt->prev)
288 /* Apparently crt1 for the application is responsible for handling this.
289 * We only need to run the init/fini for shared libraries
291 if (tpnt->libtype == program_interpreter)
293 if (tpnt->libtype == elf_executable)
295 if (tpnt->init_flag & INIT_FUNCS_CALLED)
297 tpnt->init_flag |= INIT_FUNCS_CALLED;
299 if (tpnt->dynamic_info[DT_INIT]) {
300 void (*dl_elf_func) (void);
301 dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
302 if (dl_elf_func && *dl_elf_func != NULL) {
303 #ifdef __SUPPORT_LD_DEBUG__
305 fprintf(stderr, "running ctors for library %s at '%x'\n", tpnt->libname, dl_elf_func);
312 return (void *) dyn_chain;
315 /* Something went wrong. Clean up and return NULL. */
317 do_dlclose(dyn_chain, 0);
321 void *dlsym(void *vhandle, const char *name)
323 struct elf_resolve *tpnt, *tfrom;
324 struct dyn_elf *handle;
326 struct dyn_elf *rpnt;
329 handle = (struct dyn_elf *) vhandle;
331 /* First of all verify that we have a real handle
332 of some kind. Return NULL if not a valid handle. */
335 handle = _dl_symbol_tables;
336 else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
337 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
341 _dl_error_number = LD_BAD_HANDLE;
344 } else if (handle == RTLD_NEXT) {
346 * Try and locate the module we were called from - we
347 * need this so that we know where to start searching
348 * from. We never pass RTLD_NEXT down into the actual
349 * dynamic loader itself, as it doesn't know
350 * how to properly treat it.
352 from = (ElfW(Addr)) __builtin_return_address(0);
355 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
357 if (tpnt->loadaddr < from
358 && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
365 ret = _dl_find_hash((char*)name, handle, 0);
371 _dl_error_number = LD_NO_SYMBOL;
375 static int do_dlclose(void *vhandle, int need_fini)
377 struct dyn_elf *rpnt, *rpnt1;
379 struct elf_resolve *tpnt;
380 int (*dl_elf_fini) (void);
381 void (*dl_brk) (void);
382 struct dyn_elf *handle;
386 handle = (struct dyn_elf *) vhandle;
388 for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
395 _dl_error_number = LD_BAD_HANDLE;
399 rpnt1->next_handle = rpnt->next_handle;
401 _dl_handles = rpnt->next_handle;
402 /* OK, this is a valid handle - now close out the file */
403 for (rpnt = handle; rpnt; rpnt = rpnt->next) {
405 if (--tpnt->usage_count == 0) {
406 if (need_fini && tpnt->dynamic_info[DT_FINI]) {
407 dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
412 for (i = 0, ppnt = tpnt->ppnt;
413 i < tpnt->n_phent; ppnt++, i++) {
414 if (ppnt->p_type != PT_LOAD)
416 if (end < ppnt->p_vaddr + ppnt->p_memsz)
417 end = ppnt->p_vaddr + ppnt->p_memsz;
419 _dl_munmap((void*)tpnt->loadaddr, end);
420 /* Next, remove tpnt from the loaded_module list */
421 if (_dl_loaded_modules == tpnt) {
422 _dl_loaded_modules = tpnt->next;
423 if (_dl_loaded_modules)
424 _dl_loaded_modules->prev = 0;
426 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
427 if (tpnt->next == rpnt->dyn) {
428 tpnt->next = tpnt->next->next;
430 tpnt->next->prev = tpnt;
434 /* Next, remove tpnt from the global symbol table list */
435 if (_dl_symbol_tables->dyn == rpnt->dyn) {
436 _dl_symbol_tables = rpnt->next;
437 if (_dl_symbol_tables)
438 _dl_symbol_tables->prev = 0;
440 for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
441 if (rpnt1->next->dyn == rpnt->dyn) {
443 rpnt1->next = rpnt1->next->next;
445 rpnt1->next->prev = rpnt1;
449 free(rpnt->dyn->libname);
456 if (_dl_debug_addr) {
457 dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
458 if (dl_brk != NULL) {
459 _dl_debug_addr->r_state = RT_DELETE;
462 _dl_debug_addr->r_state = RT_CONSISTENT;
470 int dlclose(void *vhandle)
472 return do_dlclose(vhandle, 1);
475 const char *dlerror(void)
479 if (!_dl_error_number)
481 retval = dl_error_names[_dl_error_number];
482 _dl_error_number = 0;
487 * Dump information to stderrr about the current loaded modules
489 static char *type[] = { "Lib", "Exe", "Int", "Mod" };
493 struct elf_resolve *tpnt;
494 struct dyn_elf *rpnt, *hpnt;
496 fprintf(stderr, "List of loaded modules\n");
497 /* First start with a complete list of all of the loaded files. */
498 for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
499 fprintf(stderr, "\t%x %x %x %s %d %s\n",
500 (unsigned) tpnt->loadaddr, (unsigned) tpnt,
501 (unsigned) tpnt->symbol_scope,
503 tpnt->usage_count, tpnt->libname);
506 /* Next dump the module list for the application itself */
507 fprintf(stderr, "\nModules for application (%x):\n",
508 (unsigned) _dl_symbol_tables);
509 for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
510 fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname);
512 for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
513 fprintf(stderr, "Modules for handle %x\n", (unsigned) hpnt);
514 for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
515 fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn,
520 int dladdr(void *__address, Dl_info * __dlip)
522 struct elf_resolve *pelf;
523 struct elf_resolve *rpnt;
528 * Try and locate the module address is in
533 fprintf(stderr, "dladdr( %x, %x )\n", __address, __dlip);
536 for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
537 struct elf_resolve *tpnt;
541 fprintf(stderr, "Module \"%s\" at %x\n",
542 tpnt->libname, tpnt->loadaddr);
544 if (tpnt->loadaddr < (ElfW(Addr)) __address
545 && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
555 * Try and locate the symbol of address
567 symtab = (Elf32_Sym *) (pelf->dynamic_info[DT_SYMTAB] + pelf->loadaddr);
568 strtab = (char *) (pelf->dynamic_info[DT_STRTAB] + pelf->loadaddr);
571 for (hn = 0; hn < pelf->nbucket; hn++) {
572 for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
573 ElfW(Addr) symbol_addr;
575 symbol_addr = pelf->loadaddr + symtab[si].st_value;
576 if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
582 fprintf(stderr, "Symbol \"%s\" at %x\n",
583 strtab + symtab[si].st_name, symbol_addr);
589 __dlip->dli_fname = pelf->libname;
590 __dlip->dli_fbase = (void *)pelf->loadaddr;
591 __dlip->dli_sname = strtab + symtab[sn].st_name;
592 __dlip->dli_saddr = (void *)sa;