123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- /* vi: set sw=4 ts=4: */
- /*
- * Program to load an ELF binary on a linux system, and run it
- * after resolving ELF shared library symbols
- *
- * Copyright (C) 2004 by Joakim Tjernlund <joakim.tjernlund@lumentis.se>
- * Copyright (C) 2000-2004 by Erik Andersen <andersen@codpoet.org>
- * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
- * David Engel, Hongjiu Lu and Mitch D'Souza
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. The name of the above contributors may not be
- * used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
- /* Various symbol table handling functions, including symbol lookup */
- /*
- * This is the start of the linked list that describes all of the files present
- * in the system with pointers to all of the symbol, string, and hash tables,
- * as well as all of the other good stuff in the binary.
- */
- struct elf_resolve *_dl_loaded_modules = NULL;
- /*
- * This is the list of modules that are loaded when the image is first
- * started. As we add more via dlopen, they get added into other
- * chains.
- */
- struct dyn_elf *_dl_symbol_tables = NULL;
- /*
- * This is the list of modules that are loaded via dlopen. We may need
- * to search these for RTLD_GLOBAL files.
- */
- struct dyn_elf *_dl_handles = NULL;
- /* This is the hash function that is used by the ELF linker to generate the
- * hash table that each executable and library is required to have. We need
- * it to decode the hash table. */
- unsigned long _dl_elf_hash(const char *name)
- {
- unsigned long hash = 0;
- unsigned long tmp;
- while (*name) {
- hash = (hash << 4) + *name++;
- if ((tmp = hash & 0xf0000000))
- hash ^= tmp >> 24;
- hash &= ~tmp;
- };
- return hash;
- }
- /* Check to see if a library has already been added to the hash chain. */
- struct elf_resolve *_dl_check_hashed_files(const char *libname)
- {
- struct elf_resolve *tpnt;
- int len = _dl_strlen(libname);
- for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
- if (_dl_strncmp(tpnt->libname, libname, len) == 0 &&
- (tpnt->libname[len] == '\0' || tpnt->libname[len] == '.'))
- return tpnt;
- }
- return NULL;
- }
- /*
- * We call this function when we have just read an ELF library or executable.
- * We add the relevant info to the symbol chain, so that we can resolve all
- * externals properly.
- */
- struct elf_resolve *_dl_add_elf_hash_table(const char *libname,
- char *loadaddr, unsigned long *dynamic_info, unsigned long dynamic_addr,
- unsigned long dynamic_size)
- {
- unsigned long *hash_addr;
- struct elf_resolve *tpnt;
- int i;
- if (!_dl_loaded_modules) {
- tpnt = _dl_loaded_modules = (struct elf_resolve *) _dl_malloc(sizeof(struct elf_resolve));
- _dl_memset(tpnt, 0, sizeof(struct elf_resolve));
- } else {
- tpnt = _dl_loaded_modules;
- while (tpnt->next)
- tpnt = tpnt->next;
- tpnt->next = (struct elf_resolve *) _dl_malloc(sizeof(struct elf_resolve));
- _dl_memset(tpnt->next, 0, sizeof(struct elf_resolve));
- tpnt->next->prev = tpnt;
- tpnt = tpnt->next;
- };
- tpnt->next = NULL;
- tpnt->init_flag = 0;
- tpnt->libname = _dl_strdup(libname);
- tpnt->dynamic_addr = (ElfW(Dyn) *)dynamic_addr;
- tpnt->dynamic_size = dynamic_size;
- tpnt->libtype = loaded_file;
- if (dynamic_info[DT_HASH] != 0) {
- hash_addr = (unsigned long *) (intptr_t)(dynamic_info[DT_HASH] + loadaddr);
- tpnt->nbucket = *hash_addr++;
- tpnt->nchain = *hash_addr++;
- tpnt->elf_buckets = hash_addr;
- hash_addr += tpnt->nbucket;
- tpnt->chains = hash_addr;
- }
- tpnt->loadaddr = (ElfW(Addr))loadaddr;
- for (i = 0; i < 24; i++)
- tpnt->dynamic_info[i] = dynamic_info[i];
- #ifdef __mips__
- {
- Elf32_Dyn *dpnt = (Elf32_Dyn *) dynamic_addr;
- while(dpnt->d_tag) {
- if (dpnt->d_tag == DT_MIPS_GOTSYM)
- tpnt->mips_gotsym = dpnt->d_un.d_val;
- if (dpnt->d_tag == DT_MIPS_LOCAL_GOTNO)
- tpnt->mips_local_gotno = dpnt->d_un.d_val;
- if (dpnt->d_tag == DT_MIPS_SYMTABNO)
- tpnt->mips_symtabno = dpnt->d_un.d_val;
- dpnt++;
- }
- }
- #endif
- return tpnt;
- }
- /*
- * This function resolves externals, and this is either called when we process
- * relocations or when we call an entry in the PLT table for the first time.
- */
- char *_dl_find_hash(const char *name, struct dyn_elf *rpnt1,
- struct elf_resolve *f_tpnt, enum caller_type caller_type)
- {
- struct elf_resolve *tpnt;
- int si;
- int pass;
- char *strtab;
- Elf32_Sym *symtab;
- unsigned long elf_hash_number, hn;
- char *weak_result;
- struct dyn_elf *rpnt, first;
- const ElfW(Sym) *sym;
- weak_result = 0;
- elf_hash_number = _dl_elf_hash(name);
- /* A quick little hack to make sure that any symbol in the executable
- will be preferred to one in a shared library. This is necessary so
- that any shared library data symbols referenced in the executable
- will be seen at the same address by the executable, shared libraries
- and dynamically loaded code. -Rob Ryan (robr@cmu.edu) */
- if (_dl_symbol_tables && rpnt1) {
- first = (*_dl_symbol_tables);
- first.next = rpnt1;
- rpnt1 = (&first);
- }
- /*
- * The passes are so that we can first search the regular symbols
- * for whatever module was specified, and then search anything
- * loaded with RTLD_GLOBAL. When pass is 1, it means we are just
- * starting the first dlopened module, and anything above that
- * is just the next one in the chain.
- */
- for (pass = 0; (1 == 1); pass++) {
- /*
- * If we are just starting to search for RTLD_GLOBAL, setup
- * the pointer for the start of the search.
- */
- if (pass == 1) {
- rpnt1 = _dl_handles;
- }
- /*
- * Anything after this, we need to skip to the next module.
- */
- else if (pass >= 2) {
- rpnt1 = rpnt1->next_handle;
- }
- /*
- * Make sure we still have a module, and make sure that this
- * module was loaded with RTLD_GLOBAL.
- */
- if (pass != 0) {
- if (rpnt1 == NULL)
- break;
- //if ((rpnt1->flags & RTLD_GLOBAL) == 0)
- //continue;
- }
- for (rpnt = (rpnt1 ? rpnt1 : _dl_symbol_tables); rpnt; rpnt = rpnt->next) {
- tpnt = rpnt->dyn;
- /* Don't search the executable when resolving a copy reloc. */
- if (tpnt->libtype == elf_executable && caller_type == copyrel)
- continue;
- /*
- * Avoid calling .urem here.
- */
- do_rem(hn, elf_hash_number, tpnt->nbucket);
- symtab = (Elf32_Sym *) (intptr_t) (tpnt->dynamic_info[DT_SYMTAB] + tpnt->loadaddr);
- strtab = (char *) (tpnt->dynamic_info[DT_STRTAB] + tpnt->loadaddr);
- for (si = tpnt->elf_buckets[hn]; si != STN_UNDEF; si = tpnt->chains[si]) {
- sym = &symtab[si];
- if (sym->st_value == 0)
- continue;
- if (ELF32_ST_TYPE(sym->st_info) > STT_FUNC)
- continue;
- if (sym->st_shndx == SHN_UNDEF && caller_type != copyrel)
- continue;
- if (_dl_strcmp(strtab + sym->st_name, name) != 0)
- continue;
- switch (ELF32_ST_BIND(sym->st_info)) {
- case STB_WEAK:
- //Disable this to match current glibc behavior. Of course,
- //this doesn't actually work yet and will cause segfaults...
- #if 1
- if (!weak_result)
- weak_result = (char *)tpnt->loadaddr + sym->st_value;
- break;
- #endif
- case STB_GLOBAL:
- return (char*)tpnt->loadaddr + sym->st_value;
- default: /* Local symbols not handled here */
- break;
- }
- }
- }
- }
- return weak_result;
- }
|