resolve.c revision 1.41
1/* $OpenBSD: resolve.c,v 1.41 2005/10/07 01:26:34 kurt 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; 41elf_object_t *_dl_loading_object; 42 43/* 44 * Add a new dynamic object to the object list. 45 */ 46void 47_dl_add_object(elf_object_t *object) 48{ 49 50 /* 51 * if this is a new object, prev will be NULL 52 * != NULL if an object already in the list 53 * prev == NULL for the first item in the list, but that will 54 * be the executable. 55 */ 56 if (object->prev != NULL) 57 return; 58 59 if (_dl_objects == NULL) { /* First object ? */ 60 _dl_last_object = _dl_objects = object; 61 } else { 62 _dl_last_object->next = object; 63 object->prev = _dl_last_object; 64 _dl_last_object = object; 65 } 66} 67 68/* 69 * Initialize a new dynamic object. 70 */ 71elf_object_t * 72_dl_finalize_object(const char *objname, Elf_Dyn *dynp, const long *dl_data, 73 const int objtype, const long laddr, const long loff) 74{ 75 elf_object_t *object; 76#if 0 77 _dl_printf("objname [%s], dynp %p, dl_data %p, objtype %x laddr %lx, loff %lx\n", 78 objname, dynp, dl_data, objtype, laddr, loff); 79#endif 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 if (_dl_loading_object == NULL) { 145 /* 146 * no loading object, object is the loading object, 147 * as it is either executable, or dlopened() 148 */ 149 _dl_loading_object = object->load_object = object; 150 DL_DEB(("head %s\n", object->load_name )); 151 } else { 152 object->load_object = _dl_loading_object; 153 } 154 DL_DEB(("obj %s has %s as head\n", object->load_name, 155 _dl_loading_object->load_name )); 156 /* refcount handled in _dl_link_sub & _dl_boot */ 157 object->refcount = 0; 158 TAILQ_INIT(&object->child_list); 159 object->opencount = 0; /* # dlopen() & exe */ 160 object->grprefcount = 0; 161 /* default dev, inode for dlopen-able objects. */ 162 object->dev = 0; 163 object->inode = 0; 164 TAILQ_INIT(&object->grpsym_list); 165 TAILQ_INIT(&object->grpref_list); 166 167 return(object); 168} 169 170void 171_dl_tailq_free(struct dep_node *n) 172{ 173 struct dep_node *next; 174 175 while (n != NULL) { 176 next = TAILQ_NEXT(n, next_sib); 177 _dl_free(n); 178 n = next; 179 } 180} 181 182elf_object_t *free_objects; 183 184void _dl_cleanup_objects(void); 185void 186_dl_cleanup_objects() 187{ 188 elf_object_t *nobj, *head; 189 struct dep_node *n, *next; 190 191 n = TAILQ_FIRST(&_dlopened_child_list); 192 while (n != NULL) { 193 next = TAILQ_NEXT(n, next_sib); 194 if (n->data->opencount + n->data->grprefcount == 0) { 195 TAILQ_REMOVE(&_dlopened_child_list, n, next_sib); 196 _dl_free(n); 197 } 198 n = next; 199 } 200 201 head = free_objects; 202 free_objects = NULL; 203 while (head != NULL) { 204 if (head->load_name) 205 _dl_free(head->load_name); 206 _dl_tailq_free(TAILQ_FIRST(&head->grpsym_list)); 207 _dl_tailq_free(TAILQ_FIRST(&head->child_list)); 208 _dl_tailq_free(TAILQ_FIRST(&head->grpref_list)); 209 nobj = head->next; 210 _dl_free(head); 211 head = nobj; 212 } 213} 214 215void 216_dl_remove_object(elf_object_t *object) 217{ 218 object->prev->next = object->next; 219 if (object->next) 220 object->next->prev = object->prev; 221 222 if (_dl_last_object == object) 223 _dl_last_object = object->prev; 224 225 object->next = free_objects; 226 free_objects = object; 227} 228 229 230elf_object_t * 231_dl_lookup_object(const char *name) 232{ 233 elf_object_t *object; 234 235 object = _dl_objects; 236 while (object) { 237 if (_dl_strcmp(name, object->load_name) == 0) 238 return(object); 239 object = object->next; 240 } 241 return(0); 242} 243 244int _dl_find_symbol_obj(elf_object_t *object, const char *name, 245 unsigned long hash, int flags, const Elf_Sym **ref, 246 const Elf_Sym **weak_sym, 247 elf_object_t **weak_object); 248 249sym_cache *_dl_symcache; 250int _dl_symcachestat_hits; 251int _dl_symcachestat_lookups; 252 253Elf_Addr 254_dl_find_symbol_bysym(elf_object_t *req_obj, unsigned int symidx, 255 const Elf_Sym **this, int flags, const Elf_Sym *ref_sym, const elf_object_t **pobj) 256{ 257 Elf_Addr ret; 258 const Elf_Sym *sym; 259 const char *symn; 260 const elf_object_t *sobj; 261 262 _dl_symcachestat_lookups ++; 263 if (_dl_symcache != NULL && 264 symidx < req_obj->nchains && 265 _dl_symcache[symidx].obj != NULL && 266 _dl_symcache[symidx].sym != NULL && 267 _dl_symcache[symidx].flags == flags) { 268 269 _dl_symcachestat_hits++; 270 sobj = _dl_symcache[symidx].obj; 271 *this = _dl_symcache[symidx].sym; 272 if (pobj) 273 *pobj = sobj; 274 return sobj->load_offs; 275 } 276 277 sym = req_obj->dyn.symtab; 278 sym += symidx; 279 symn = req_obj->dyn.strtab + sym->st_name; 280 281 ret = _dl_find_symbol(symn, this, flags, ref_sym, req_obj, &sobj); 282 283 if (pobj) 284 *pobj = sobj; 285 286 if (_dl_symcache != NULL && symidx < req_obj->nchains) { 287 _dl_symcache[symidx].sym = *this; 288 _dl_symcache[symidx].obj = sobj; 289 _dl_symcache[symidx].flags = flags; 290 } 291 292 return ret; 293} 294 295Elf_Addr 296_dl_find_symbol(const char *name, const Elf_Sym **this, 297 int flags, const Elf_Sym *ref_sym, elf_object_t *req_obj, 298 const elf_object_t **pobj) 299{ 300 const Elf_Sym *weak_sym = NULL; 301 unsigned long h = 0; 302 const char *p = name; 303 elf_object_t *object = NULL, *weak_object = NULL; 304 int found = 0; 305 struct dep_node *n, *m; 306 307 308 while (*p) { 309 unsigned long g; 310 h = (h << 4) + *p++; 311 if ((g = h & 0xf0000000)) 312 h ^= g >> 24; 313 h &= ~g; 314 } 315 316 if (req_obj->dyn.symbolic) 317 if (_dl_find_symbol_obj(req_obj, name, h, flags, this, &weak_sym, 318 &weak_object)) { 319 object = req_obj; 320 found = 1; 321 goto found; 322 } 323 324 if (flags & SYM_SEARCH_OBJ) { 325 if (_dl_find_symbol_obj(req_obj, name, h, flags, this, 326 &weak_sym, &weak_object)) { 327 object = req_obj; 328 found = 1; 329 } 330 } else if (flags & SYM_DLSYM) { 331 if (_dl_find_symbol_obj(req_obj, name, h, flags, this, 332 &weak_sym, &weak_object)) { 333 object = req_obj; 334 found = 1; 335 } 336 if (weak_object != NULL && found == 0) { 337 object=weak_object; 338 *this = weak_sym; 339 found = 1; 340 } 341 /* search dlopened obj and all children */ 342 343 if (found == 0) { 344 TAILQ_FOREACH(n, &req_obj->load_object->grpsym_list, 345 next_sib) { 346 if (_dl_find_symbol_obj(n->data, name, h, 347 flags, this, 348 &weak_sym, &weak_object)) { 349 object = n->data; 350 found = 1; 351 break; 352 } 353 } 354 } 355 } else { 356 int skip = 0; 357 358 if ((flags & SYM_SEARCH_SELF) || (flags & SYM_SEARCH_NEXT)) 359 skip = 1; 360 361 /* 362 * search dlopened objects: global or req_obj == dlopened_obj 363 * and and it's children 364 */ 365 TAILQ_FOREACH(n, &_dlopened_child_list, next_sib) { 366 if (((n->data->obj_flags & RTLD_GLOBAL) == 0) && 367 (n->data != req_obj->load_object)) 368 continue; 369 370 TAILQ_FOREACH(m, &n->data->grpsym_list, next_sib) { 371 if (skip == 1) { 372 if (m->data == req_obj) { 373 skip = 0; 374 if (flags & SYM_SEARCH_NEXT) 375 continue; 376 } else 377 continue; 378 } 379 if ((flags & SYM_SEARCH_OTHER) && 380 (m->data == req_obj)) 381 continue; 382 if (_dl_find_symbol_obj(m->data, name, h, flags, 383 this, &weak_sym, &weak_object)) { 384 object = m->data; 385 found = 1; 386 goto found; 387 } 388 } 389 } 390 } 391 392found: 393 if (weak_object != NULL && found == 0) { 394 object=weak_object; 395 *this = weak_sym; 396 found = 1; 397 } 398 399 400 if (found == 0) { 401 if ((ref_sym == NULL || 402 (ELF_ST_BIND(ref_sym->st_info) != STB_WEAK)) && 403 (flags & SYM_WARNNOTFOUND)) 404 _dl_printf("%s:%s: undefined symbol '%s'\n", 405 _dl_progname, req_obj->load_name, name); 406 return (0); 407 } 408 409 if (ref_sym != NULL && ref_sym->st_size != 0 && 410 (ref_sym->st_size != (*this)->st_size) && 411 (ELF_ST_TYPE((*this)->st_info) != STT_FUNC) ) { 412 _dl_printf("%s:%s: %s : WARNING: " 413 "symbol(%s) size mismatch, relink your program\n", 414 _dl_progname, req_obj->load_name, 415 object->load_name, name); 416 } 417 418 if (pobj) 419 *pobj = object; 420 421 return (object->load_offs); 422} 423 424int 425_dl_find_symbol_obj(elf_object_t *object, const char *name, unsigned long hash, 426 int flags, const Elf_Sym **this, const Elf_Sym **weak_sym, 427 elf_object_t **weak_object) 428{ 429 const Elf_Sym *symt = object->dyn.symtab; 430 const char *strt = object->dyn.strtab; 431 long si; 432 const char *symn; 433 434 for (si = object->buckets[hash % object->nbuckets]; 435 si != STN_UNDEF; si = object->chains[si]) { 436 const Elf_Sym *sym = symt + si; 437 438 if (sym->st_value == 0) 439 continue; 440 441 if (ELF_ST_TYPE(sym->st_info) != STT_NOTYPE && 442 ELF_ST_TYPE(sym->st_info) != STT_OBJECT && 443 ELF_ST_TYPE(sym->st_info) != STT_FUNC) 444 continue; 445 446 symn = strt + sym->st_name; 447 if (sym != *this && _dl_strcmp(symn, name)) 448 continue; 449 450 /* allow this symbol if we are referring to a function 451 * which has a value, even if section is UNDEF. 452 * this allows &func to refer to PLT as per the 453 * ELF spec. st_value is checked above. 454 * if flags has SYM_PLT set, we must have actual 455 * symbol, so this symbol is skipped. 456 */ 457 if (sym->st_shndx == SHN_UNDEF) { 458 if ((flags & SYM_PLT) || sym->st_value == 0 || 459 ELF_ST_TYPE(sym->st_info) != STT_FUNC) 460 continue; 461 } 462 463 if (ELF_ST_BIND(sym->st_info) == STB_GLOBAL) { 464 *this = sym; 465 return 1; 466 } else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) { 467 if (!*weak_sym) { 468 *weak_sym = sym; 469 *weak_object = object; 470 } 471 } 472 } 473 return 0; 474} 475