resolve.c revision 1.1
1/* $OpenBSD: resolve.c,v 1.1 2000/06/13 03:34:07 rahnds Exp $ */ 2 3/* 4 * Copyright (c) 1998 Per Fogelstrom, Opsycon AB 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed under OpenBSD by 17 * Per Fogelstrom, Opsycon AB, Sweden. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 */ 34 35#define _DYN_LOADER 36 37#include <sys/types.h> 38 39#include <link.h> 40#include "resolve.h" 41#include "syscall.h" 42#include "archdep.h" 43 44elf_object_t *_dl_objects; 45elf_object_t *_dl_last_object; 46 47 48/* 49 * Initialize and add a new dynamic object to the object list. 50 * Perform necessary relocations of pointers. 51 */ 52 53elf_object_t * 54_dl_add_object(const char *objname, Elf32_Dyn *dynp, 55 const u_int32_t *dl_data, const int objtype, 56 const int laddr, const int loff) 57{ 58 elf_object_t *object; 59 60 object = (elf_object_t *)_dl_malloc(sizeof(elf_object_t)); 61 62 if(_dl_objects == 0) { /* First object ? */ 63 _dl_last_object = _dl_objects = object; 64 } 65 else { 66 _dl_last_object->next = object; 67 object->prev = _dl_last_object; 68 _dl_last_object = object; 69 } 70 71 object->load_dyn = dynp; 72 while(dynp->d_tag != DT_NULL) { 73 if(dynp->d_tag < DT_PROCNUM) { 74 object->Dyn.info[dynp->d_tag] = dynp->d_un.d_val; 75 } 76 else if(dynp->d_tag >= DT_LOPROC && dynp->d_tag < DT_LOPROC + DT_PROCNUM) { 77 object->Dyn.info[dynp->d_tag + DT_NUM - DT_LOPROC] = dynp-> 78d_un.d_val; 79 } 80 if(dynp->d_tag == DT_TEXTREL) 81 object->dyn.textrel = 1; 82 if(dynp->d_tag == DT_SYMBOLIC) 83 object->dyn.symbolic = 1; 84 if(dynp->d_tag == DT_BIND_NOW) 85 object->dyn.bind_now = 1; 86 dynp++; 87 } 88 89 /* 90 * Now relocate all pointer to dynamic info, but only 91 * the ones which has pointer values. 92 */ 93 if(object->Dyn.info[DT_PLTGOT]) 94 object->Dyn.info[DT_PLTGOT] += loff; 95 if(object->Dyn.info[DT_HASH]) 96 object->Dyn.info[DT_HASH] += loff; 97 if(object->Dyn.info[DT_STRTAB]) 98 object->Dyn.info[DT_STRTAB] += loff; 99 if(object->Dyn.info[DT_SYMTAB]) 100 object->Dyn.info[DT_SYMTAB] += loff; 101 if(object->Dyn.info[DT_RELA]) 102 object->Dyn.info[DT_RELA] += loff; 103 if(object->Dyn.info[DT_SONAME]) 104 object->Dyn.info[DT_SONAME] += loff; 105 if(object->Dyn.info[DT_RPATH]) 106 object->Dyn.info[DT_RPATH] += loff; 107 if(object->Dyn.info[DT_REL]) 108 object->Dyn.info[DT_REL] += loff; 109 if(object->Dyn.info[DT_INIT]) 110 object->Dyn.info[DT_INIT] += loff; 111 if(object->Dyn.info[DT_FINI]) 112 object->Dyn.info[DT_FINI] += loff; 113 114 object->buckets = object->dyn.hash; 115 if(object->buckets != 0) { 116 object->nbuckets = *object->buckets++; 117 object->nchains = *object->buckets++; 118 object->chains = object->buckets + object->nbuckets; 119 } 120 121 if(dl_data) { 122 object->phdrp = (Elf32_Phdr *) dl_data[AUX_phdr]; 123 object->phdrc = dl_data[AUX_phnum]; 124 } 125 object->obj_type = objtype; 126 object->load_addr = laddr; 127 object->load_offs = loff; 128 object->load_name = (char *)_dl_malloc(_dl_strlen(objname) + 1); 129 _dl_strcpy(object->load_name, objname); 130 object->refcount = 1; 131 132 return(object); 133} 134 135 136void 137_dl_remove_object(elf_object_t *object) 138{ 139 elf_object_t *depobj; 140 141 object->prev->next = object->next; 142 if(object->next) { 143 object->next->prev = object->prev; 144 } 145 if(object->load_name) { 146 _dl_free(object->load_name); 147 } 148 while((depobj = object->dep_next)) { 149 object->dep_next = object->dep_next->dep_next; 150 _dl_free(depobj); 151 } 152 _dl_free(object); 153} 154 155 156elf_object_t * 157_dl_lookup_object(const char *name) 158{ 159 elf_object_t *object; 160 161 object = _dl_objects; 162 while(object) { 163 if(_dl_strcmp(name, object->load_name) == 0) { 164 return(object); 165 } 166 object = object->next; 167 } 168 return(0); 169} 170 171 172Elf32_Addr 173_dl_find_symbol(const char *name, elf_object_t *startlook, 174 const Elf32_Sym **ref, int myself) 175{ 176 u_int32_t h = 0; 177 const char *p = name; 178 elf_object_t *object; 179 const Elf32_Sym *weak_sym = 0; 180 Elf32_Addr weak_offs = 0; 181 182 while(*p) { 183 u_int32_t g; 184 h = (h << 4) + *p++; 185 if((g = h & 0xf0000000)) { 186 h ^= g >> 24; 187 } 188 h &= ~g; 189 } 190 191 for(object = startlook; object; object = (myself ? 0 : object->next)) { 192 const Elf32_Sym *symt; 193 const char *strt; 194 u_int32_t si; 195 196 symt = object->dyn.symtab; 197 strt = object->dyn.strtab; 198 199 for(si = object->buckets[h % object->nbuckets]; 200 si != STN_UNDEF; si = object->chains[si]) { 201 const Elf32_Sym *sym = symt + si; 202 203 if(sym->st_value == 0 || 204 sym->st_shndx == SHN_UNDEF) { 205 continue; 206 } 207 208 if(ELF32_ST_TYPE(sym->st_info) != STT_NOTYPE && 209 ELF32_ST_TYPE(sym->st_info) != STT_OBJECT && 210 ELF32_ST_TYPE(sym->st_info) != STT_FUNC) { 211 continue; 212 } 213 214 if(sym != *ref && _dl_strcmp(strt+sym->st_name, name)) { 215 continue; 216 } 217 218 if(ELF32_ST_BIND(sym->st_info) == STB_GLOBAL) { 219 *ref = sym; 220 return(object->load_offs); 221 } 222 else if(ELF32_ST_BIND(sym->st_info) == STB_WEAK) { 223 if(!weak_sym) { 224 weak_sym = sym; 225 weak_offs = object->load_offs; 226 } 227 } 228 } 229 } 230 if(!weak_sym && *ref && ELF32_ST_BIND((*ref)->st_info) != STB_WEAK) { 231 _dl_printf("%s: undefined symbol '%s'\n", _dl_progname, name); 232 } 233 *ref = weak_sym; 234 return(weak_offs); 235} 236 237