1 /* $OpenBSD: library_subr.c,v 1.34 2011/07/13 20:49:44 drahn Exp $ */ 2 3 /* 4 * Copyright (c) 2002 Dale Rahn 5 * Copyright (c) 1998 Per Fogelstrom, Opsycon AB 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 */ 29 30 #define _DYN_LOADER 31 32 #include <sys/types.h> 33 #include <sys/syslimits.h> 34 #include <sys/param.h> 35 #include <sys/queue.h> 36 #include <dirent.h> 37 #include <string.h> 38 39 #include "archdep.h" 40 #include "resolve.h" 41 #include "dir.h" 42 #include "sod.h" 43 44 #define DEFAULT_PATH "/usr/lib" 45 46 47 /* STATIC DATA */ 48 struct dlochld _dlopened_child_list; 49 50 51 /* 52 * _dl_match_file() 53 * 54 * This fucntion determines if a given name matches what is specified 55 * in a struct sod. The major must match exactly, and the minor must 56 * be same or larger. 57 * 58 * sodp is updated with the minor if this matches. 59 */ 60 61 int 62 _dl_match_file(struct sod *sodp, const char *name, int namelen) 63 { 64 int match; 65 struct sod lsod; 66 const char *lname; 67 68 lname = name; 69 if (sodp->sod_library) { 70 if (_dl_strncmp(name, "lib", 3)) 71 return 0; 72 lname += 3; 73 } 74 if (_dl_strncmp(lname, (char *)sodp->sod_name, 75 _dl_strlen((char *)sodp->sod_name))) 76 return 0; 77 78 _dl_build_sod(name, &lsod); 79 80 match = 0; 81 if ((_dl_strcmp((char *)lsod.sod_name, (char *)sodp->sod_name) == 0) && 82 (lsod.sod_library == sodp->sod_library) && 83 ((sodp->sod_major == -1) || (sodp->sod_major == lsod.sod_major)) && 84 ((sodp->sod_minor == -1) || 85 (lsod.sod_minor >= sodp->sod_minor))) { 86 match = 1; 87 88 /* return version matched */ 89 sodp->sod_major = lsod.sod_major; 90 sodp->sod_minor = lsod.sod_minor; 91 } 92 _dl_free((char *)lsod.sod_name); 93 return match; 94 } 95 96 /* 97 * _dl_cmp_sod() 98 * 99 * This fucntion compares sod structs. The major must match exactly, 100 * and the minor must be same or larger. 101 * 102 * sodp is updated with the minor if this matches. 103 */ 104 105 int 106 _dl_cmp_sod(struct sod *sodp, const struct sod *lsod) 107 { 108 int match; 109 110 match = 1; 111 if ((_dl_strcmp((char *)lsod->sod_name, (char *)sodp->sod_name) == 0) && 112 (lsod->sod_library == sodp->sod_library) && 113 ((sodp->sod_major == -1) || (sodp->sod_major == lsod->sod_major)) && 114 ((sodp->sod_minor == -1) || 115 (lsod->sod_minor >= sodp->sod_minor))) { 116 match = 0; 117 118 /* return version matched */ 119 sodp->sod_major = lsod->sod_major; 120 sodp->sod_minor = lsod->sod_minor; 121 } 122 return match; 123 } 124 125 char _dl_hint_store[MAXPATHLEN]; 126 127 char * 128 _dl_find_shlib(struct sod *sodp, const char *searchpath, int nohints) 129 { 130 char *hint, lp[PATH_MAX + 10], *path; 131 struct dirent *dp; 132 const char *pp; 133 int match, len; 134 DIR *dd; 135 struct sod tsod, bsod; /* transient and best sod */ 136 137 /* if we are to search default directories, and hints 138 * are not to be used, search the standard path from ldconfig 139 * (_dl_hint_search_path) or use the default path 140 */ 141 if (nohints) 142 goto nohints; 143 144 if (searchpath == NULL) { 145 /* search 'standard' locations, find any match in the hints */ 146 hint = _dl_findhint((char *)sodp->sod_name, sodp->sod_major, 147 sodp->sod_minor, NULL); 148 if (hint) 149 return hint; 150 } else { 151 /* search hints requesting matches for only 152 * the searchpath directories, 153 */ 154 pp = searchpath; 155 while (pp) { 156 path = lp; 157 while (path < lp + PATH_MAX && 158 *pp && *pp != ':' && *pp != ';') 159 *path++ = *pp++; 160 *path = 0; 161 162 /* interpret "" as curdir "." */ 163 if (lp[0] == '\0') { 164 lp[0] = '.'; 165 lp[1] = '\0'; 166 } 167 168 hint = _dl_findhint((char *)sodp->sod_name, 169 sodp->sod_major, sodp->sod_minor, lp); 170 if (hint != NULL) 171 return hint; 172 173 if (*pp) /* Try curdir if ':' at end */ 174 pp++; 175 else 176 pp = 0; 177 } 178 } 179 180 /* 181 * For each directory in the searchpath, read the directory 182 * entries looking for a match to sod. filename compare is 183 * done by _dl_match_file() 184 */ 185 nohints: 186 if (searchpath == NULL) { 187 if (_dl_hint_search_path != NULL) 188 searchpath = _dl_hint_search_path; 189 else 190 searchpath = DEFAULT_PATH; 191 } 192 _dl_memset(&bsod, 0, sizeof(bsod)); 193 pp = searchpath; 194 while (pp) { 195 path = lp; 196 while (path < lp + PATH_MAX && *pp && *pp != ':' && *pp != ';') 197 *path++ = *pp++; 198 *path = 0; 199 200 /* interpret "" as curdir "." */ 201 if (lp[0] == '\0') { 202 lp[0] = '.'; 203 lp[1] = '\0'; 204 } 205 206 if ((dd = _dl_opendir(lp)) != NULL) { 207 match = 0; 208 while ((dp = _dl_readdir(dd)) != NULL) { 209 tsod = *sodp; 210 if (_dl_match_file(&tsod, dp->d_name, 211 dp->d_namlen)) { 212 /* 213 * When a match is found, tsod is 214 * updated with the major+minor found. 215 * This version is compared with the 216 * largest so far (kept in bsod), 217 * and saved if larger. 218 */ 219 if (!match || 220 tsod.sod_major == -1 || 221 tsod.sod_major > bsod.sod_major || 222 ((tsod.sod_major == 223 bsod.sod_major) && 224 tsod.sod_minor > bsod.sod_minor)) { 225 bsod = tsod; 226 match = 1; 227 len = _dl_strlcpy( 228 _dl_hint_store, lp, 229 MAXPATHLEN); 230 if (lp[len-1] != '/') { 231 _dl_hint_store[len] = 232 '/'; 233 len++; 234 } 235 _dl_strlcpy( 236 &_dl_hint_store[len], 237 dp->d_name, 238 MAXPATHLEN-len); 239 if (tsod.sod_major == -1) 240 break; 241 } 242 } 243 } 244 _dl_closedir(dd); 245 if (match) { 246 *sodp = bsod; 247 return (_dl_hint_store); 248 } 249 } 250 251 if (*pp) /* Try curdir if ':' at end */ 252 pp++; 253 else 254 pp = 0; 255 } 256 return NULL; 257 } 258 259 elf_object_t * 260 _dl_lookup_object(const char *req_name, struct sod *req_sod) 261 { 262 elf_object_t *object = _dl_objects; 263 264 while (object) { 265 char *soname; 266 267 if (_dl_cmp_sod(req_sod, &object->sod) == 0) 268 return(object); 269 270 soname = (char *)object->Dyn.info[DT_SONAME]; 271 if (soname != NULL) { 272 if (_dl_strcmp(req_name, soname) == 0) 273 return(object); 274 } 275 276 object = object->next; 277 } 278 279 return(NULL); 280 } 281 282 elf_object_t * 283 _dl_find_loaded_shlib(const char *req_name, struct sod req_sod, int flags) 284 { 285 elf_object_t *object; 286 287 object = _dl_lookup_object(req_name, &req_sod); 288 289 /* if not found retry with any minor */ 290 if (object == NULL && req_sod.sod_library && req_sod.sod_minor != -1) { 291 short orig_minor = req_sod.sod_minor; 292 req_sod.sod_minor = -1; 293 object = _dl_lookup_object(req_name, &req_sod); 294 295 if (object != NULL && req_sod.sod_minor < orig_minor) 296 _dl_printf("warning: lib%s.so.%d.%d: " 297 "minor version >= %d expected, " 298 "using it anyway\n", 299 req_sod.sod_name, req_sod.sod_major, 300 req_sod.sod_minor, orig_minor); 301 } 302 303 if (object) { /* Already loaded */ 304 object->obj_flags |= flags & RTLD_GLOBAL; 305 if (_dl_loading_object == NULL) 306 _dl_loading_object = object; 307 if (object->load_object != _dl_objects && 308 object->load_object != _dl_loading_object) { 309 _dl_link_grpref(object->load_object, _dl_loading_object); 310 } 311 } 312 313 return (object); 314 } 315 316 /* 317 * Load a shared object. Search order is: 318 * First check loaded objects for a matching shlib, otherwise: 319 * 320 * If the name contains a '/' use only the path preceding the 321 * library name and do not continue on to other methods if not 322 * found. 323 * search hints for match in path preceding library name 324 * this will only match specific library version. 325 * search path preceding library name 326 * this will find largest minor version in path provided 327 * try the LD_LIBRARY_PATH specification (if present) 328 * search hints for match in LD_LIBRARY_PATH dirs 329 * this will only match specific libary version. 330 * search LD_LIBRARY_PATH dirs for match. 331 * this will find largest minor version in first dir found. 332 * check DT_RPATH paths, (if present) 333 * search hints for match in DT_RPATH dirs 334 * this will only match specific libary version. 335 * search DT_RPATH dirs for match. 336 * this will find largest minor version in first dir found. 337 * last look in default search directory, either as specified 338 * by ldconfig or default to '/usr/lib' 339 */ 340 341 342 elf_object_t * 343 _dl_load_shlib(const char *libname, elf_object_t *parent, int type, int flags) 344 { 345 int try_any_minor, ignore_hints; 346 struct sod sod, req_sod; 347 elf_object_t *object = NULL; 348 char *hint; 349 350 try_any_minor = 0; 351 ignore_hints = 0; 352 353 if (_dl_strchr(libname, '/')) { 354 char *lpath, *lname; 355 lpath = _dl_strdup(libname); 356 lname = _dl_strrchr(lpath, '/'); 357 if (lname == NULL) { 358 _dl_free(lpath); 359 _dl_errno = DL_NOT_FOUND; 360 return (object); 361 } 362 *lname = '\0'; 363 lname++; 364 if (*lname == '\0') { 365 _dl_free(lpath); 366 _dl_errno = DL_NOT_FOUND; 367 return (object); 368 } 369 370 _dl_build_sod(lname, &sod); 371 req_sod = sod; 372 373 fullpathagain: 374 hint = _dl_find_shlib(&req_sod, lpath, ignore_hints); 375 if (hint != NULL) 376 goto fullpathdone; 377 378 if (try_any_minor == 0) { 379 try_any_minor = 1; 380 ignore_hints = 1; 381 req_sod.sod_minor = -1; 382 goto fullpathagain; 383 } 384 _dl_errno = DL_NOT_FOUND; 385 fullpathdone: 386 _dl_free(lpath); 387 goto done; 388 } 389 390 _dl_build_sod(libname, &sod); 391 req_sod = sod; 392 393 object = _dl_find_loaded_shlib(libname, req_sod, flags); 394 if (object) { 395 _dl_free((char *)sod.sod_name); 396 return (object); 397 } 398 399 again: 400 /* No '/' in name. Scan the known places, LD_LIBRARY_PATH first. */ 401 if (_dl_libpath != NULL) { 402 hint = _dl_find_shlib(&req_sod, _dl_libpath, ignore_hints); 403 if (hint != NULL) 404 goto done; 405 } 406 407 /* Check DT_RPATH. */ 408 if (parent->dyn.rpath != NULL) { 409 hint = _dl_find_shlib(&req_sod, parent->dyn.rpath, ignore_hints); 410 if (hint != NULL) 411 goto done; 412 } 413 414 /* Check main program's DT_RPATH, if parent != main program */ 415 if (parent != _dl_objects && _dl_objects->dyn.rpath != NULL) { 416 hint = _dl_find_shlib(&req_sod, _dl_objects->dyn.rpath, ignore_hints); 417 if (hint != NULL) 418 goto done; 419 } 420 421 /* check 'standard' locations */ 422 hint = _dl_find_shlib(&req_sod, NULL, ignore_hints); 423 if (hint != NULL) 424 goto done; 425 426 if (try_any_minor == 0) { 427 try_any_minor = 1; 428 ignore_hints = 1; 429 req_sod.sod_minor = -1; 430 goto again; 431 } 432 _dl_errno = DL_NOT_FOUND; 433 done: 434 if (hint != NULL) { 435 if (req_sod.sod_minor < sod.sod_minor) 436 _dl_printf("warning: lib%s.so.%d.%d: " 437 "minor version >= %d expected, " 438 "using it anyway\n", 439 sod.sod_name, sod.sod_major, 440 req_sod.sod_minor, sod.sod_minor); 441 object = _dl_tryload_shlib(hint, type, flags); 442 } 443 _dl_free((char *)sod.sod_name); 444 return(object); 445 } 446 447 448 void 449 _dl_link_dlopen(elf_object_t *dep) 450 { 451 struct dep_node *n; 452 453 dep->opencount++; 454 455 if (OBJECT_DLREF_CNT(dep) > 1) 456 return; 457 458 n = _dl_malloc(sizeof *n); 459 if (n == NULL) 460 _dl_exit(5); 461 462 n->data = dep; 463 TAILQ_INSERT_TAIL(&_dlopened_child_list, n, next_sib); 464 465 DL_DEB(("linking %s as dlopen()ed\n", dep->load_name)); 466 } 467 468 void 469 _dl_child_refcnt_decrement(elf_object_t *object) 470 { 471 struct dep_node *n; 472 473 object->refcount--; 474 if (OBJECT_REF_CNT(object) == 0) 475 TAILQ_FOREACH(n, &object->child_list, next_sib) 476 _dl_child_refcnt_decrement(n->data); 477 } 478 479 void 480 _dl_notify_unload_shlib(elf_object_t *object) 481 { 482 struct dep_node *n; 483 484 if (OBJECT_REF_CNT(object) == 0) 485 TAILQ_FOREACH(n, &object->child_list, next_sib) 486 _dl_child_refcnt_decrement(n->data); 487 488 if (OBJECT_DLREF_CNT(object) == 0) { 489 while ((n = TAILQ_FIRST(&object->grpref_list)) != NULL) { 490 TAILQ_REMOVE(&object->grpref_list, n, next_sib); 491 n->data->grprefcount--; 492 _dl_notify_unload_shlib(n->data); 493 _dl_free(n); 494 } 495 } 496 } 497 498 void 499 _dl_unload_dlopen(void) 500 { 501 struct dep_node *node; 502 503 TAILQ_FOREACH_REVERSE(node, &_dlopened_child_list, dlochld, next_sib) { 504 /* dont dlclose the main program */ 505 if (node->data == _dl_objects) 506 continue; 507 508 while (node->data->opencount > 0) { 509 node->data->opencount--; 510 _dl_notify_unload_shlib(node->data); 511 _dl_run_all_dtors(); 512 } 513 } 514 } 515 516 void 517 _dl_link_grpref(elf_object_t *load_group, elf_object_t *load_object) 518 { 519 struct dep_node *n; 520 521 n = _dl_malloc(sizeof *n); 522 if (n == NULL) 523 _dl_exit(7); 524 n->data = load_group; 525 TAILQ_INSERT_TAIL(&load_object->grpref_list, n, next_sib); 526 load_group->grprefcount++; 527 } 528 529 void 530 _dl_link_child(elf_object_t *dep, elf_object_t *p) 531 { 532 struct dep_node *n; 533 534 n = _dl_malloc(sizeof *n); 535 if (n == NULL) 536 _dl_exit(7); 537 n->data = dep; 538 TAILQ_INSERT_TAIL(&p->child_list, n, next_sib); 539 540 dep->refcount++; 541 542 DL_DEB(("linking dep %s as child of %s\n", dep->load_name, 543 p->load_name)); 544 } 545 546 void 547 _dl_link_grpsym(elf_object_t *object, int checklist) 548 { 549 struct dep_node *n; 550 551 if (checklist) { 552 TAILQ_FOREACH(n, &_dl_loading_object->grpsym_list, next_sib) 553 if (n->data == object) 554 return; /* found, dont bother adding */ 555 } else { 556 if (object->lastlookup == _dl_searchnum) { 557 return; /* found, dont bother adding */ 558 } 559 } 560 object->lastlookup = _dl_searchnum; 561 562 n = _dl_malloc(sizeof *n); 563 if (n == NULL) 564 _dl_exit(8); 565 n->data = object; 566 TAILQ_INSERT_TAIL(&_dl_loading_object->grpsym_list, n, next_sib); 567 } 568 569 void 570 _dl_cache_grpsym_list_setup(elf_object_t *object) 571 { 572 _dl_newsymsearch(); 573 _dl_cache_grpsym_list(object); 574 } 575 void 576 _dl_cache_grpsym_list(elf_object_t *object) 577 { 578 struct dep_node *n; 579 580 /* 581 * grpsym_list is an ordered list of all child libs of the 582 * _dl_loading_object with no dups. The order is equalivant 583 * to a breath-first traversal of the child list without dups. 584 */ 585 586 TAILQ_FOREACH(n, &object->child_list, next_sib) 587 _dl_link_grpsym(n->data, 0); 588 589 TAILQ_FOREACH(n, &object->child_list, next_sib) 590 _dl_cache_grpsym_list(n->data); 591 } 592