resolve.c revision 1.23
1/* $OpenBSD: resolve.c,v 1.23 2004/05/25 18:07:20 mickey 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 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 */ 28 29#define _DYN_LOADER 30 31#include <sys/types.h> 32 33#include <nlist.h> 34#include <link.h> 35#include "syscall.h" 36#include "archdep.h" 37#include "resolve.h" 38 39elf_object_t *_dl_objects; 40elf_object_t *_dl_last_object; 41 42/* 43 * Add a new dynamic object to the object list. 44 */ 45void 46_dl_add_object(elf_object_t *object) 47{ 48 49 /* 50 * if this is a new object, prev will be NULL 51 * != NULL if an object already in the list 52 * prev == NULL for the first item in the list, but that will 53 * be the executable. 54 */ 55 if (object->prev != NULL) 56 return; 57 58 if (_dl_objects == NULL) { /* First object ? */ 59 _dl_last_object = _dl_objects = object; 60 } else { 61 _dl_last_object->next = object; 62 object->prev = _dl_last_object; 63 _dl_last_object = object; 64 } 65} 66 67/* 68 * Initialize a new dynamic object. 69 */ 70elf_object_t * 71_dl_finalize_object(const char *objname, Elf_Dyn *dynp, const u_long *dl_data, 72 const int objtype, const long laddr, const long loff) 73{ 74 elf_object_t *object; 75#if 0 76 _dl_printf("objname [%s], dynp %p, dl_data %p, objtype %x laddr %lx, loff %lx\n", 77 objname, dynp, dl_data, objtype, laddr, loff); 78#endif 79 80 object = _dl_malloc(sizeof(elf_object_t)); 81 object->prev = object->next = NULL; 82 83 object->load_dyn = dynp; 84 while (dynp->d_tag != DT_NULL) { 85 if (dynp->d_tag < DT_NUM) 86 object->Dyn.info[dynp->d_tag] = dynp->d_un.d_val; 87 else if (dynp->d_tag >= DT_LOPROC && 88 dynp->d_tag < DT_LOPROC + DT_NUM) 89 object->Dyn.info[dynp->d_tag + DT_NUM - DT_LOPROC] = 90 dynp->d_un.d_val; 91 if (dynp->d_tag == DT_TEXTREL) 92 object->dyn.textrel = 1; 93 if (dynp->d_tag == DT_SYMBOLIC) 94 object->dyn.symbolic = 1; 95 if (dynp->d_tag == DT_BIND_NOW) 96 object->dyn.bind_now = 1; 97 dynp++; 98 } 99 100 /* 101 * Now relocate all pointer to dynamic info, but only 102 * the ones which have pointer values. 103 */ 104 if (object->Dyn.info[DT_PLTGOT]) 105 object->Dyn.info[DT_PLTGOT] += loff; 106 if (object->Dyn.info[DT_HASH]) 107 object->Dyn.info[DT_HASH] += loff; 108 if (object->Dyn.info[DT_STRTAB]) 109 object->Dyn.info[DT_STRTAB] += loff; 110 if (object->Dyn.info[DT_SYMTAB]) 111 object->Dyn.info[DT_SYMTAB] += loff; 112 if (object->Dyn.info[DT_RELA]) 113 object->Dyn.info[DT_RELA] += loff; 114 if (object->Dyn.info[DT_SONAME]) 115 object->Dyn.info[DT_SONAME] += loff; 116 if (object->Dyn.info[DT_RPATH]) 117 object->Dyn.info[DT_RPATH] += object->Dyn.info[DT_STRTAB]; 118 if (object->Dyn.info[DT_REL]) 119 object->Dyn.info[DT_REL] += loff; 120 if (object->Dyn.info[DT_INIT]) 121 object->Dyn.info[DT_INIT] += loff; 122 if (object->Dyn.info[DT_FINI]) 123 object->Dyn.info[DT_FINI] += loff; 124 if (object->Dyn.info[DT_JMPREL]) 125 object->Dyn.info[DT_JMPREL] += loff; 126 127 if (object->Dyn.info[DT_HASH] != 0) { 128 Elf_Word *hashtab = (Elf_Word *)object->Dyn.info[DT_HASH]; 129 130 object->nbuckets = hashtab[0]; 131 object->nchains = hashtab[1]; 132 object->buckets = hashtab + 2; 133 object->chains = object->buckets + object->nbuckets; 134 } 135 136 if (dl_data) { 137 object->phdrp = (Elf_Phdr *) dl_data[AUX_phdr]; 138 object->phdrc = dl_data[AUX_phnum]; 139 } 140 object->obj_type = objtype; 141 object->load_addr = laddr; 142 object->load_offs = loff; 143 object->load_name = _dl_strdup(objname); 144 object->refcount = 1; 145 object->first_child = NULL; 146 object->last_child = NULL; 147 return(object); 148} 149 150void 151_dl_remove_object(elf_object_t *object) 152{ 153 elf_object_t *depobj; 154 155 object->prev->next = object->next; 156 if (object->next) 157 object->next->prev = object->prev; 158 159 if (_dl_last_object == object) 160 _dl_last_object = object->prev; 161 162 if (object->load_name) 163 _dl_free(object->load_name); 164 165 while ((depobj = object->dep_next)) { 166 object->dep_next = object->dep_next->dep_next; 167 _dl_free(depobj); 168 } 169 _dl_free(object); 170} 171 172 173elf_object_t * 174_dl_lookup_object(const char *name) 175{ 176 elf_object_t *object; 177 178 object = _dl_objects; 179 while (object) { 180 if (_dl_strcmp(name, object->load_name) == 0) 181 return(object); 182 object = object->next; 183 } 184 return(0); 185} 186 187int find_symbol_obj(elf_object_t *object, const char *name, unsigned long hash, 188 int flags, const Elf_Sym **ref, const Elf_Sym **weak_sym, 189 elf_object_t **weak_object); 190 191sym_cache *_dl_symcache; 192int _dl_symcachestat_hits; 193int _dl_symcachestat_lookups; 194 195Elf_Addr 196_dl_find_symbol_bysym(elf_object_t *req_obj, unsigned int symidx, 197 elf_object_t *startlook, const Elf_Sym **ref, const elf_object_t **pobj, 198 int flags, int req_size) 199{ 200 Elf_Addr ret; 201 const Elf_Sym *sym; 202 const char *symn; 203 const elf_object_t *sobj; 204 205 _dl_symcachestat_lookups ++; 206 if ((_dl_symcache != NULL) && 207 (symidx < req_obj->nchains) && 208 (_dl_symcache[symidx].obj != NULL) && 209 (_dl_symcache[symidx].sym != NULL) && 210 (_dl_symcache[symidx].flags == flags)) { 211 212 _dl_symcachestat_hits++; 213 sobj = _dl_symcache[symidx].obj; 214 *ref = _dl_symcache[symidx].sym; 215 if (pobj) 216 *pobj = sobj; 217 return sobj->load_offs; 218 } 219 220 sym = req_obj->dyn.symtab; 221 sym += symidx; 222 symn = req_obj->dyn.strtab + sym->st_name; 223 224 ret = _dl_find_symbol(symn, startlook, ref, &sobj, 225 flags, req_size, req_obj); 226 227 if (pobj) 228 *pobj = sobj; 229 230 if ((_dl_symcache != NULL) && 231 (symidx < req_obj->nchains)) { 232 _dl_symcache[symidx].sym = *ref; 233 _dl_symcache[symidx].obj = sobj; 234 _dl_symcache[symidx].flags = flags; 235 } 236 237 return ret; 238} 239 240Elf_Addr 241_dl_find_symbol(const char *name, elf_object_t *startlook, 242 const Elf_Sym **ref, const elf_object_t **pobj, 243 int flags, int req_size, elf_object_t *req_obj) 244{ 245 const Elf_Sym *weak_sym = NULL; 246 unsigned long h = 0; 247 const char *p = name; 248 elf_object_t *object, *weak_object = NULL; 249 int found = 0; 250 int lastchance = 0; 251 252 while (*p) { 253 unsigned long g; 254 h = (h << 4) + *p++; 255 if ((g = h & 0xf0000000)) 256 h ^= g >> 24; 257 h &= ~g; 258 } 259 260 if (req_obj->dyn.symbolic) 261 if (find_symbol_obj(req_obj, name, h, flags, ref, &weak_sym, 262 &weak_object)) { 263 object = req_obj; 264 found = 1; 265 goto found; 266 } 267 268retry_nonglobal_dlo: 269 for (object = startlook; object; 270 object = ((flags & SYM_SEARCH_SELF) ? 0 : object->next)) { 271 272 if ((lastchance == 0) && 273 ((object->obj_flags & RTLD_GLOBAL) == 0) && 274 (object->obj_type == OBJTYPE_DLO) && 275 (object != req_obj)) 276 continue; 277 278 if (find_symbol_obj(object, name, h, flags, ref, &weak_sym, 279 &weak_object)) { 280 found = 1; 281 break; 282 } 283 } 284 285found: 286 if (weak_object != NULL && found == 0) { 287 object=weak_object; 288 *ref = weak_sym; 289 found = 1; 290 } 291 292 if (found == 0) { 293 if (lastchance == 0) { 294 lastchance = 1; 295 goto retry_nonglobal_dlo; 296 } 297 if (flags & SYM_WARNNOTFOUND) 298 _dl_printf("%s:%s: undefined symbol '%s'\n", 299 _dl_progname, req_obj->load_name, name); 300 return (0); 301 } 302 303 if (req_size != (*ref)->st_size && req_size != 0 && 304 (ELF_ST_TYPE((*ref)->st_info) != STT_FUNC)) { 305 _dl_printf("%s:%s: %s : WARNING: " 306 "symbol(%s) size mismatch, relink your program\n", 307 _dl_progname, req_obj->load_name, 308 object->load_name, name); 309 } 310 311 if (pobj) 312 *pobj = object; 313 314 return (object->load_offs); 315} 316 317int 318find_symbol_obj(elf_object_t *object, const char *name, unsigned long hash, 319 int flags, const Elf_Sym **ref, const Elf_Sym **weak_sym, 320 elf_object_t **weak_object) 321{ 322 const Elf_Sym *symt = object->dyn.symtab; 323 const char *strt = object->dyn.strtab; 324 long si; 325 const char *symn; 326 327 for (si = object->buckets[hash % object->nbuckets]; 328 si != STN_UNDEF; si = object->chains[si]) { 329 const Elf_Sym *sym = symt + si; 330 331 if (sym->st_value == 0) 332 continue; 333 334 if (ELF_ST_TYPE(sym->st_info) != STT_NOTYPE && 335 ELF_ST_TYPE(sym->st_info) != STT_OBJECT && 336 ELF_ST_TYPE(sym->st_info) != STT_FUNC) 337 continue; 338 339 symn = strt + sym->st_name; 340 if (sym != *ref && _dl_strcmp(symn, name)) 341 continue; 342 343 /* allow this symbol if we are referring to a function 344 * which has a value, even if section is UNDEF. 345 * this allows &func to refer to PLT as per the 346 * ELF spec. st_value is checked above. 347 * if flags has SYM_PLT set, we must have actual 348 * symbol, so this symbol is skipped. 349 */ 350 if (sym->st_shndx == SHN_UNDEF) { 351 if ((flags & SYM_PLT) || sym->st_value == 0 || 352 ELF_ST_TYPE(sym->st_info) != STT_FUNC) 353 continue; 354 } 355 356 if (ELF_ST_BIND(sym->st_info) == STB_GLOBAL) { 357 *ref = sym; 358 return 1; 359 } else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) { 360 if (!*weak_sym) { 361 *weak_sym = sym; 362 *weak_object = object; 363 } 364 } 365 } 366 return 0; 367} 368