1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * Copyright (c) 1988 AT&T 29 * All Rights Reserved 30 */ 31 32 /* 33 * Programmatic interface to the run_time linker. 34 */ 35 36 #include <sys/debug.h> 37 #include <stdio.h> 38 #include <string.h> 39 #include <dlfcn.h> 40 #include <synch.h> 41 #include <limits.h> 42 #include <debug.h> 43 #include "_rtld.h" 44 #include "_audit.h" 45 #include "_elf.h" 46 #include "_inline.h" 47 #include "msg.h" 48 49 /* 50 * Determine who called us - given a pc determine in which object it resides. 51 * 52 * For dlopen() the link map of the caller must be passed to load_so() so that 53 * the appropriate search rules (4.x or 5.0) are used to locate any 54 * dependencies. Also, if we've been called from a 4.x module it may be 55 * necessary to fix the specified pathname so that it conforms with the 5.0 elf 56 * rules. 57 * 58 * For dlsym() the link map of the caller is used to determine RTLD_NEXT 59 * requests, together with requests based off of a dlopen(0). 60 * For dladdr() this routines provides a generic means of scanning all loaded 61 * segments. 62 */ 63 Rt_map * 64 _caller(caddr_t cpc, int flags) 65 { 66 Lm_list *lml; 67 Listnode *lnp; 68 69 for (LIST_TRAVERSE(&dynlm_list, lnp, lml)) { 70 Aliste idx; 71 Lm_cntl *lmc; 72 73 for (ALIST_TRAVERSE(lml->lm_lists, idx, lmc)) { 74 Rt_map *lmp; 75 76 for (lmp = lmc->lc_head; lmp; 77 lmp = NEXT_RT_MAP(lmp)) { 78 79 if (find_segment(cpc, lmp)) 80 return (lmp); 81 } 82 } 83 } 84 85 /* 86 * No mapping can be determined. If asked for a default, assume this 87 * is from the executable. 88 */ 89 if (flags & CL_EXECDEF) 90 return ((Rt_map *)lml_main.lm_head); 91 92 return (0); 93 } 94 95 #pragma weak _dlerror = dlerror 96 97 /* 98 * External entry for dlerror(3dl). Returns a pointer to the string describing 99 * the last occurring error. The last occurring error is cleared. 100 */ 101 char * 102 dlerror() 103 { 104 char *error; 105 Rt_map *clmp; 106 int entry; 107 108 entry = enter(0); 109 110 clmp = _caller(caller(), CL_EXECDEF); 111 112 error = lasterr; 113 lasterr = NULL; 114 115 if (entry) 116 leave(LIST(clmp), 0); 117 return (error); 118 } 119 120 /* 121 * Add a dependency as a group descriptor to a group handle. Returns 0 on 122 * failure, ALE_EXISTS if the dependency already exists, or ALE_CREATE if it 123 * is newly created. 124 */ 125 int 126 hdl_add(Grp_hdl *ghp, Rt_map *lmp, uint_t flags) 127 { 128 Grp_desc *gdp; 129 Aliste idx; 130 int found = ALE_CREATE; 131 uint_t oflags; 132 133 /* 134 * Make sure this dependency hasn't already been recorded. 135 */ 136 for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) { 137 if (gdp->gd_depend == lmp) { 138 found = ALE_EXISTS; 139 break; 140 } 141 } 142 143 if (found == ALE_CREATE) { 144 Grp_desc gd; 145 146 /* 147 * Create a new handle descriptor. 148 */ 149 gd.gd_depend = lmp; 150 gd.gd_flags = 0; 151 152 /* 153 * Indicate this object is a part of this handles group. 154 */ 155 if (aplist_append(&GROUPS(lmp), ghp, AL_CNT_GROUPS) == NULL) 156 return (0); 157 158 /* 159 * Append the new dependency to this handle. 160 */ 161 if ((gdp = alist_append(&ghp->gh_depends, &gd, 162 sizeof (Grp_desc), AL_CNT_DEPENDS)) == NULL) 163 return (0); 164 } 165 166 oflags = gdp->gd_flags; 167 gdp->gd_flags |= flags; 168 169 if (DBG_ENABLED) { 170 if (found == ALE_CREATE) 171 DBG_CALL(Dbg_file_hdl_action(ghp, lmp, DBG_DEP_ADD, 172 gdp->gd_flags)); 173 else if (gdp->gd_flags != oflags) 174 DBG_CALL(Dbg_file_hdl_action(ghp, lmp, DBG_DEP_UPDATE, 175 gdp->gd_flags)); 176 } 177 return (found); 178 } 179 180 /* 181 * Create a handle. 182 */ 183 Grp_hdl * 184 hdl_create(Lm_list *lml, Rt_map *nlmp, Rt_map *clmp, uint_t hflags, 185 uint_t ndflags, uint_t cdflags) 186 { 187 Grp_hdl *ghp = 0, *_ghp; 188 APlist **alpp; 189 Aliste idx; 190 191 /* 192 * For dlopen(0) the handle is maintained as part of the link-map list, 193 * otherwise it is associated with the referenced link-map. 194 */ 195 if (hflags & GPH_ZERO) 196 alpp = &(lml->lm_handle); 197 else 198 alpp = &(HANDLES(nlmp)); 199 200 /* 201 * Objects can contain multiple handles depending on the handle flags 202 * supplied. Most RTLD flags pertain to the object itself and the 203 * bindings that it can achieve. Multiple handles for these flags 204 * don't make sense. But if the flag determines how the handle might 205 * be used, then multiple handles may exist. Presently this only makes 206 * sense for RTLD_FIRST. Determine if an appropriate handle already 207 * exists. 208 */ 209 for (APLIST_TRAVERSE(*alpp, idx, _ghp)) { 210 if ((_ghp->gh_flags & GPH_FIRST) == (hflags & GPH_FIRST)) { 211 ghp = _ghp; 212 break; 213 } 214 } 215 216 if (ghp == 0) { 217 uint_t ndx; 218 219 DBG_CALL(Dbg_file_hdl_title(DBG_HDL_CREATE)); 220 221 /* 222 * If this is the first dlopen() request for this handle 223 * allocate and initialize a new handle. 224 */ 225 if ((ghp = malloc(sizeof (Grp_hdl))) == NULL) 226 return (NULL); 227 228 /* 229 * Associate the handle with the link-map list or the reference 230 * link-map as appropriate. 231 */ 232 if (aplist_append(alpp, ghp, AL_CNT_GROUPS) == NULL) { 233 free(ghp); 234 return (NULL); 235 } 236 237 /* 238 * Record the existence of this handle for future verification. 239 */ 240 /* LINTED */ 241 ndx = (uintptr_t)ghp % HDLIST_SZ; 242 243 if (aplist_append(&hdl_alp[ndx], ghp, AL_CNT_HANDLES) == NULL) { 244 (void) aplist_delete_value(*alpp, ghp); 245 free(ghp); 246 return (NULL); 247 } 248 249 ghp->gh_depends = NULL; 250 ghp->gh_refcnt = 1; 251 ghp->gh_flags = hflags; 252 253 /* 254 * A dlopen(0) handle is identified by the GPH_ZERO flag, the 255 * head of the link-map list is defined as the owner. There is 256 * no need to maintain a list of dependencies, for when this 257 * handle is used (for dlsym()) a dynamic search through the 258 * entire link-map list provides for searching all objects with 259 * GLOBAL visibility. 260 */ 261 if (hflags & GPH_ZERO) { 262 ghp->gh_ownlmp = lml->lm_head; 263 ghp->gh_ownlml = lml; 264 } else { 265 ghp->gh_ownlmp = nlmp; 266 ghp->gh_ownlml = LIST(nlmp); 267 268 if (hdl_add(ghp, nlmp, ndflags) == 0) 269 return (NULL); 270 271 /* 272 * Indicate that a local group now exists. This state 273 * allows singleton searches to be optimized. 274 */ 275 if ((hflags & GPH_LDSO) == 0) 276 LIST(nlmp)->lm_flags |= LML_FLG_GROUPSEXIST; 277 } 278 } else { 279 /* 280 * If a handle already exists, bump its reference count. 281 * 282 * If the previous reference count was 0, then this is a handle 283 * that an earlier call to dlclose() was unable to remove. Such 284 * handles are put on the orphan list. As this handle is back 285 * in use, it must be removed from the orphan list. 286 * 287 * Note, handles associated with a link-map list itself (i.e. 288 * dlopen(0)) can have a reference count of 0. However, these 289 * handles are never deleted, and therefore are never moved to 290 * the orphan list. 291 */ 292 if ((ghp->gh_refcnt++ == 0) && 293 ((ghp->gh_flags & GPH_ZERO) == 0)) { 294 uint_t ndx; 295 296 /* LINTED */ 297 ndx = (uintptr_t)ghp % HDLIST_SZ; 298 299 (void) aplist_delete_value(hdl_alp[HDLIST_ORP], ghp); 300 (void) aplist_append(&hdl_alp[ndx], ghp, 301 AL_CNT_HANDLES); 302 303 if (DBG_ENABLED) { 304 Aliste idx; 305 Grp_desc *gdp; 306 307 DBG_CALL(Dbg_file_hdl_title(DBG_HDL_REINST)); 308 for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) 309 DBG_CALL(Dbg_file_hdl_action(ghp, 310 gdp->gd_depend, DBG_DEP_REINST, 0)); 311 } 312 } 313 } 314 315 /* 316 * Keep track of the parent (caller). As this object could be opened 317 * by different parents, this processing is carried out every time a 318 * handle is requested. 319 */ 320 if (clmp && (hdl_add(ghp, clmp, cdflags) == 0)) 321 return (NULL); 322 323 return (ghp); 324 } 325 326 /* 327 * Initialize a handle that has been created for an object that is already 328 * loaded. The handle is initialized with the present dependencies of that 329 * object. Once this initialization has occurred, any new objects that might 330 * be loaded as dependencies (lazy-loading) are added to the handle as each new 331 * object is loaded. 332 */ 333 int 334 hdl_initialize(Grp_hdl *ghp, Rt_map *nlmp, int mode, int promote) 335 { 336 Aliste idx; 337 Grp_desc *gdp; 338 339 /* 340 * If the handle has already been initialized, and the initial object's 341 * mode hasn't been promoted, there's no need to recompute the modes of 342 * any dependencies. If the object we've added has just been opened, 343 * the objects dependencies will not yet have been processed. These 344 * dependencies will be added on later calls to load_one(). Otherwise, 345 * this object already exists, so add all of its dependencies to the 346 * handle were operating on. 347 */ 348 if (((ghp->gh_flags & GPH_INITIAL) && (promote == 0)) || 349 ((FLAGS(nlmp) & FLG_RT_ANALYZED) == 0)) { 350 ghp->gh_flags |= GPH_INITIAL; 351 return (1); 352 } 353 354 DBG_CALL(Dbg_file_hdl_title(DBG_HDL_ADD)); 355 for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) { 356 Rt_map * lmp = gdp->gd_depend; 357 Aliste idx1; 358 Bnd_desc *bdp; 359 360 /* 361 * If this dependency doesn't indicate that its dependencies 362 * should be added to a handle, ignore it. This case identifies 363 * a parent of a dlopen(RTLD_PARENT) request. 364 */ 365 if ((gdp->gd_flags & GPD_ADDEPS) == 0) 366 continue; 367 368 for (APLIST_TRAVERSE(DEPENDS(lmp), idx1, bdp)) { 369 Rt_map *dlmp = bdp->b_depend; 370 371 if ((bdp->b_flags & BND_NEEDED) == 0) 372 continue; 373 374 if (hdl_add(ghp, dlmp, 375 (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS)) == 0) 376 return (0); 377 378 (void) update_mode(dlmp, MODE(dlmp), mode); 379 } 380 } 381 ghp->gh_flags |= GPH_INITIAL; 382 return (1); 383 } 384 385 /* 386 * Sanity check a program-provided handle. 387 */ 388 static int 389 hdl_validate(Grp_hdl *ghp) 390 { 391 Aliste idx; 392 Grp_hdl *lghp; 393 uint_t ndx; 394 395 /* LINTED */ 396 ndx = (uintptr_t)ghp % HDLIST_SZ; 397 398 for (APLIST_TRAVERSE(hdl_alp[ndx], idx, lghp)) { 399 if ((lghp == ghp) && (ghp->gh_refcnt != 0)) 400 return (1); 401 } 402 return (0); 403 } 404 405 /* 406 * Core dlclose activity. 407 */ 408 int 409 dlclose_core(Grp_hdl *ghp, Rt_map *clmp, Lm_list *lml) 410 { 411 int error; 412 413 /* 414 * If we're already at atexit() there's no point processing further, 415 * all objects have already been tsorted for fini processing. 416 */ 417 if ((rtld_flags & RT_FL_ATEXIT) != 0) 418 return (0); 419 420 /* 421 * Diagnose what we're up to. 422 */ 423 if (ghp->gh_flags & GPH_ZERO) { 424 DBG_CALL(Dbg_file_dlclose(LIST(clmp), MSG_ORIG(MSG_STR_ZERO), 425 DBG_DLCLOSE_IGNORE)); 426 } else { 427 DBG_CALL(Dbg_file_dlclose(LIST(clmp), NAME(ghp->gh_ownlmp), 428 DBG_DLCLOSE_NULL)); 429 } 430 431 432 /* 433 * Decrement reference count of this object. 434 */ 435 if (--(ghp->gh_refcnt)) 436 return (0); 437 438 /* 439 * If this handle is special (dlopen(0)), then leave it around - it 440 * has little overhead. 441 */ 442 if (ghp->gh_flags & GPH_ZERO) 443 return (0); 444 445 /* 446 * This handle is no longer being referenced, remove it. If this handle 447 * is part of an alternative link-map list, determine if the whole list 448 * can be removed also. 449 */ 450 error = remove_hdl(ghp, clmp, 0); 451 452 if ((lml->lm_flags & (LML_FLG_BASELM | LML_FLG_RTLDLM)) == 0) 453 remove_lml(lml); 454 455 return (error); 456 } 457 458 /* 459 * Internal dlclose activity. Called from user level or directly for internal 460 * error cleanup. 461 */ 462 int 463 dlclose_intn(Grp_hdl *ghp, Rt_map *clmp) 464 { 465 Rt_map *nlmp = 0; 466 Lm_list *olml = 0; 467 int error; 468 469 /* 470 * Although we're deleting object(s) it's quite possible that additional 471 * objects get loaded from running the .fini section(s) of the objects 472 * being deleted. These objects will have been added to the same 473 * link-map list as those objects being deleted. Remember this list 474 * for later investigation. 475 */ 476 olml = ghp->gh_ownlml; 477 478 error = dlclose_core(ghp, clmp, olml); 479 480 /* 481 * Determine whether the original link-map list still exists. In the 482 * case of a dlclose of an alternative (dlmopen) link-map the whole 483 * list may have been removed. 484 */ 485 if (olml) { 486 Listnode *lnp; 487 Lm_list *lml; 488 489 for (LIST_TRAVERSE(&dynlm_list, lnp, lml)) { 490 if (olml == lml) { 491 nlmp = olml->lm_head; 492 break; 493 } 494 } 495 } 496 load_completion(nlmp); 497 return (error); 498 } 499 500 /* 501 * Argument checking for dlclose. Only called via external entry. 502 */ 503 static int 504 dlclose_check(void *handle, Rt_map *clmp) 505 { 506 Grp_hdl *ghp = (Grp_hdl *)handle; 507 508 if (hdl_validate(ghp) == 0) { 509 eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL)); 510 return (1); 511 } 512 return (dlclose_intn(ghp, clmp)); 513 } 514 515 #pragma weak _dlclose = dlclose 516 517 /* 518 * External entry for dlclose(3dl). Returns 0 for success, non-zero otherwise. 519 */ 520 int 521 dlclose(void *handle) 522 { 523 int error, entry; 524 Rt_map *clmp; 525 526 entry = enter(0); 527 528 clmp = _caller(caller(), CL_EXECDEF); 529 530 error = dlclose_check(handle, clmp); 531 532 if (entry) 533 leave(LIST(clmp), 0); 534 return (error); 535 } 536 537 static uint_t lmid = 0; 538 539 /* 540 * The addition of new link-map lists is assumed to be in small quantities. 541 * Here, we assign a unique link-map id for diagnostic use. Simply update the 542 * running link-map count until we max out. 543 */ 544 int 545 newlmid(Lm_list *lml) 546 { 547 char buffer[MSG_LMID_ALT_SIZE + 12]; 548 549 if (lmid == UINT_MAX) { 550 lml->lm_lmid = UINT_MAX; 551 (void) strncpy(buffer, MSG_ORIG(MSG_LMID_MAXED), 552 MSG_LMID_ALT_SIZE + 12); 553 } else { 554 lml->lm_lmid = lmid++; 555 (void) snprintf(buffer, MSG_LMID_ALT_SIZE + 12, 556 MSG_ORIG(MSG_LMID_FMT), MSG_ORIG(MSG_LMID_ALT), 557 lml->lm_lmid); 558 } 559 if ((lml->lm_lmidstr = strdup(buffer)) == 0) 560 return (0); 561 562 return (1); 563 } 564 565 /* 566 * Core dlopen activity. 567 */ 568 static Grp_hdl * 569 dlmopen_core(Lm_list *lml, const char *path, int mode, Rt_map *clmp, 570 uint_t flags, uint_t orig, int *in_nfavl) 571 { 572 Alist *palp = NULL; 573 Rt_map *nlmp; 574 Grp_hdl *ghp; 575 Aliste olmco, nlmco; 576 Lm_cntl *lmc; 577 578 DBG_CALL(Dbg_file_dlopen(clmp, 579 (path ? path : MSG_ORIG(MSG_STR_ZERO)), in_nfavl, mode)); 580 581 /* 582 * Having diagnosed the originally defined modes, assign any defaults 583 * or corrections. 584 */ 585 if (((mode & (RTLD_GROUP | RTLD_WORLD)) == 0) && 586 ((mode & RTLD_NOLOAD) == 0)) 587 mode |= (RTLD_GROUP | RTLD_WORLD); 588 if ((mode & RTLD_NOW) && (rtld_flags2 & RT_FL2_BINDLAZY)) { 589 mode &= ~RTLD_NOW; 590 mode |= RTLD_LAZY; 591 } 592 593 /* 594 * If the path specified is null then we're operating on global 595 * objects. Associate a dummy handle with the link-map list. 596 */ 597 if (path == 0) { 598 Grp_hdl *ghp; 599 uint_t hflags = GPH_ZERO, cdflags = GPD_PARENT; 600 int promote = 0; 601 602 /* 603 * Establish any flags for the handle (Grp_hdl). 604 * 605 * . This is a dummy handle (0) that provides for a dynamic 606 * search of all global objects within the process. 607 * 608 * . Use of the RTLD_FIRST flag indicates that only the first 609 * dependency on the handle (the new object) can be used 610 * to satisfy dlsym() requests. 611 */ 612 if (mode & RTLD_FIRST) 613 hflags |= GPH_FIRST; 614 615 /* 616 * Establish the flags for this callers dependency descriptor 617 * (Grp_desc). 618 * 619 * . The explicit creation of a handle creates a descriptor 620 * for the new object and the parent (caller), 621 * 622 * . Use of the RTLD_PARENT flag indicates that the parent 623 * can be relocated against. 624 */ 625 if (mode & RTLD_PARENT) 626 cdflags |= GPD_RELOC; 627 628 if ((ghp = hdl_create(lml, 0, clmp, hflags, 629 (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS), cdflags)) == 0) 630 return (0); 631 632 /* 633 * Traverse the main link-map control list, updating the mode 634 * of any objects as necessary. Call the relocation engine if 635 * this mode promotes the existing state of any relocations. 636 * crle()'s first pass loads all objects necessary for building 637 * a configuration file, however none of them are relocated. 638 * crle()'s second pass relocates objects in preparation for 639 * dldump()'ing using dlopen(0, RTLD_NOW). 640 */ 641 if ((mode & (RTLD_NOW | RTLD_CONFGEN)) == RTLD_CONFGEN) 642 return (ghp); 643 644 for (nlmp = lml->lm_head; nlmp; nlmp = NEXT_RT_MAP(nlmp)) { 645 if (((MODE(nlmp) & RTLD_GLOBAL) == 0) || 646 (FLAGS(nlmp) & FLG_RT_DELETE)) 647 continue; 648 649 if (update_mode(nlmp, MODE(nlmp), mode)) 650 promote = 1; 651 } 652 if (promote) 653 (void) relocate_lmc(lml, ALIST_OFF_DATA, clmp, 654 lml->lm_head, in_nfavl); 655 656 return (ghp); 657 } 658 659 /* 660 * Fix the pathname. If this object expands to multiple paths (ie. 661 * $ISALIST or $HWCAP have been used), then make sure the user has also 662 * furnished the RTLD_FIRST flag. As yet, we don't support opening 663 * more than one object at a time, so enforcing the RTLD_FIRST flag 664 * provides flexibility should we be able to support dlopening more 665 * than one object in the future. 666 */ 667 if (LM_FIX_NAME(clmp)(path, clmp, &palp, AL_CNT_NEEDED, orig) == 0) 668 return (0); 669 670 if ((palp->al_arritems > 1) && ((mode & RTLD_FIRST) == 0)) { 671 remove_plist(&palp, 1); 672 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_5)); 673 return (0); 674 } 675 676 /* 677 * Create a new link-map control list for this request, and load the 678 * associated object. 679 */ 680 if ((lmc = alist_append(&lml->lm_lists, 0, sizeof (Lm_cntl), 681 AL_CNT_LMLISTS)) == 0) { 682 remove_plist(&palp, 1); 683 return (0); 684 } 685 olmco = nlmco = (Aliste)((char *)lmc - (char *)lml->lm_lists); 686 687 nlmp = load_one(lml, nlmco, palp, clmp, mode, (flags | FLG_RT_HANDLE), 688 &ghp, in_nfavl); 689 690 /* 691 * Remove any expanded pathname infrastructure, and if the dependency 692 * couldn't be loaded, cleanup. 693 */ 694 remove_plist(&palp, 1); 695 if (nlmp == 0) { 696 remove_cntl(lml, olmco); 697 return (0); 698 } 699 700 /* 701 * If loading an auditor was requested, and the auditor already existed, 702 * then the link-map returned will be to the original auditor. The new 703 * link-map list that was initially created, and the associated link-map 704 * control list are no longer needed. As the auditor is already loaded, 705 * we're probably done, but fall through in case additional relocations 706 * would be triggered by the mode of the caller. 707 */ 708 if ((flags & FLG_RT_AUDIT) && (LIST(nlmp) != lml)) { 709 remove_cntl(lml, olmco); 710 lml = LIST(nlmp); 711 olmco = 0; 712 nlmco = ALIST_OFF_DATA; 713 } 714 715 /* 716 * Finish processing the objects associated with this request. 717 */ 718 if (((nlmp = analyze_lmc(lml, nlmco, nlmp, in_nfavl)) == NULL) || 719 (relocate_lmc(lml, nlmco, clmp, nlmp, in_nfavl) == 0)) { 720 ghp = 0; 721 nlmp = NULL; 722 } 723 724 /* 725 * If this lazyload has failed, and we've created a new link-map 726 * control list to which this request has added objects, then remove 727 * all the objects that have been associated to this request. 728 */ 729 if ((nlmp == NULL) && olmco && lmc->lc_head) 730 remove_lmc(lml, clmp, lmc, olmco, path); 731 732 /* 733 * Finally, remove any link-map control list that was created. 734 */ 735 if (olmco) 736 remove_cntl(lml, olmco); 737 738 return (ghp); 739 } 740 741 /* 742 * dlopen() and dlsym() operations are the means by which a process can 743 * test for the existence of required dependencies. If the necessary 744 * dependencies don't exist, then associated functionality can't be used. 745 * However, the lack of dependencies can be fixed, and the dlopen() and 746 * dlsym() requests can be repeated. As we use a "not-found" AVL tree to 747 * cache any failed full path loads, secondary dlopen() and dlsym() requests 748 * will fail, even if the dependencies have been installed. 749 * 750 * dlopen() and dlsym() retry any failures by removing the "not-found" AVL 751 * tree. Should any dependencies be found, their names are added to the 752 * FullPath AVL tree. This routine removes any new "not-found" AVL tree, 753 * so that the dlopen() or dlsym() can replace the original "not-found" tree. 754 */ 755 inline static void 756 nfavl_remove(avl_tree_t *avlt) 757 { 758 PathNode *pnp; 759 void *cookie = NULL; 760 761 if (avlt) { 762 while ((pnp = avl_destroy_nodes(avlt, &cookie)) != NULL) 763 free(pnp); 764 765 avl_destroy(avlt); 766 free(avlt); 767 } 768 } 769 770 /* 771 * Internal dlopen() activity. Called from user level or directly for internal 772 * opens that require a handle. 773 */ 774 Grp_hdl * 775 dlmopen_intn(Lm_list *lml, const char *path, int mode, Rt_map *clmp, 776 uint_t flags, uint_t orig) 777 { 778 Rt_map *dlmp = 0; 779 Grp_hdl *ghp; 780 int in_nfavl = 0; 781 782 /* 783 * Check for magic link-map list values: 784 * 785 * LM_ID_BASE: Operate on the PRIMARY (executables) link map 786 * LM_ID_LDSO: Operation on ld.so.1's link map 787 * LM_ID_NEWLM: Create a new link-map. 788 */ 789 if (lml == (Lm_list *)LM_ID_NEWLM) { 790 if ((lml = calloc(sizeof (Lm_list), 1)) == NULL) 791 return (NULL); 792 793 /* 794 * Establish the new link-map flags from the callers and those 795 * explicitly provided. 796 */ 797 lml->lm_tflags = LIST(clmp)->lm_tflags; 798 if (flags & FLG_RT_AUDIT) { 799 /* 800 * Unset any auditing flags - an auditor shouldn't be 801 * audited. Insure all audit dependencies are loaded. 802 */ 803 lml->lm_tflags &= ~LML_TFLG_AUD_MASK; 804 lml->lm_tflags |= 805 (LML_TFLG_NOLAZYLD | LML_TFLG_LOADFLTR); 806 lml->lm_flags |= LML_FLG_NOAUDIT; 807 } 808 809 if (list_append(&dynlm_list, lml) == 0) { 810 free(lml); 811 return (NULL); 812 } 813 if (newlmid(lml) == 0) { 814 list_delete(&dynlm_list, lml); 815 free(lml); 816 return (NULL); 817 } 818 } else if ((uintptr_t)lml < LM_ID_NUM) { 819 if ((uintptr_t)lml == LM_ID_BASE) 820 lml = &lml_main; 821 else if ((uintptr_t)lml == LM_ID_LDSO) 822 lml = &lml_rtld; 823 } 824 825 /* 826 * Open the required object on the associated link-map list. 827 */ 828 ghp = dlmopen_core(lml, path, mode, clmp, flags, orig, &in_nfavl); 829 830 /* 831 * If the object could not be found it is possible that the "not-found" 832 * AVL tree had indicated that the file does not exist. In case the 833 * file system has changed since this "not-found" recording was made, 834 * retry the dlopen() with a clean "not-found" AVL tree. 835 */ 836 if ((ghp == 0) && in_nfavl) { 837 avl_tree_t *oavlt = nfavl; 838 839 nfavl = NULL; 840 ghp = dlmopen_core(lml, path, mode, clmp, flags, orig, NULL); 841 842 /* 843 * If the file is found, then its full path name will have been 844 * registered in the FullPath AVL tree. Remove any new 845 * "not-found" AVL information, and restore the former AVL tree. 846 */ 847 nfavl_remove(nfavl); 848 nfavl = oavlt; 849 } 850 851 /* 852 * Establish the new link-map from which .init processing will begin. 853 * Ignore .init firing when constructing a configuration file (crle(1)). 854 */ 855 if (ghp && ((mode & RTLD_CONFGEN) == 0)) 856 dlmp = ghp->gh_ownlmp; 857 858 /* 859 * If loading an auditor was requested, and the auditor already existed, 860 * then the link-map returned will be to the original auditor. Remove 861 * the link-map control list that was created for this request. 862 */ 863 if (dlmp && (flags & FLG_RT_AUDIT) && (LIST(dlmp) != lml)) { 864 remove_lml(lml); 865 lml = LIST(dlmp); 866 } 867 868 /* 869 * If this load failed, remove any alternative link-map list. 870 */ 871 if ((ghp == 0) && 872 ((lml->lm_flags & (LML_FLG_BASELM | LML_FLG_RTLDLM)) == 0)) { 873 remove_lml(lml); 874 lml = 0; 875 } 876 877 /* 878 * Finish this load request. If objects were loaded, .init processing 879 * is computed. Finally, the debuggers are informed of the link-map 880 * lists being stable. 881 */ 882 load_completion(dlmp); 883 884 return (ghp); 885 } 886 887 /* 888 * Argument checking for dlopen. Only called via external entry. 889 */ 890 static Grp_hdl * 891 dlmopen_check(Lm_list *lml, const char *path, int mode, Rt_map *clmp) 892 { 893 /* 894 * Verify that a valid pathname has been supplied. 895 */ 896 if (path && (*path == '\0')) { 897 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLPATH)); 898 return (0); 899 } 900 901 /* 902 * Historically we've always verified the mode is either RTLD_NOW or 903 * RTLD_LAZY. RTLD_NOLOAD is valid by itself. Use of LM_ID_NEWLM 904 * requires a specific pathname, and use of RTLD_PARENT is meaningless. 905 */ 906 if ((mode & (RTLD_NOW | RTLD_LAZY | RTLD_NOLOAD)) == 0) { 907 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_1)); 908 return (0); 909 } 910 if ((mode & (RTLD_NOW | RTLD_LAZY)) == (RTLD_NOW | RTLD_LAZY)) { 911 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_2)); 912 return (0); 913 } 914 if ((lml == (Lm_list *)LM_ID_NEWLM) && (path == 0)) { 915 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_3)); 916 return (0); 917 } 918 if ((lml == (Lm_list *)LM_ID_NEWLM) && (mode & RTLD_PARENT)) { 919 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_4)); 920 return (0); 921 } 922 923 return (dlmopen_intn(lml, path, mode, clmp, 0, 0)); 924 } 925 926 #pragma weak _dlopen = dlopen 927 928 /* 929 * External entry for dlopen(3dl). On success, returns a pointer (handle) to 930 * the structure containing information about the newly added object, ie. can 931 * be used by dlsym(). On failure, returns a null pointer. 932 */ 933 void * 934 dlopen(const char *path, int mode) 935 { 936 int entry; 937 Rt_map *clmp; 938 Grp_hdl *ghp; 939 Lm_list *lml; 940 941 entry = enter(0); 942 943 clmp = _caller(caller(), CL_EXECDEF); 944 lml = LIST(clmp); 945 946 ghp = dlmopen_check(lml, path, mode, clmp); 947 948 if (entry) 949 leave(lml, 0); 950 return ((void *)ghp); 951 } 952 953 #pragma weak _dlmopen = dlmopen 954 955 /* 956 * External entry for dlmopen(3dl). 957 */ 958 void * 959 dlmopen(Lmid_t lmid, const char *path, int mode) 960 { 961 int entry; 962 Rt_map *clmp; 963 Grp_hdl *ghp; 964 965 entry = enter(0); 966 967 clmp = _caller(caller(), CL_EXECDEF); 968 969 ghp = dlmopen_check((Lm_list *)lmid, path, mode, clmp); 970 971 if (entry) 972 leave(LIST(clmp), 0); 973 return ((void *)ghp); 974 } 975 976 /* 977 * Handle processing for dlsym. 978 */ 979 Sym * 980 dlsym_handle(Grp_hdl *ghp, Slookup *slp, Rt_map **_lmp, uint_t *binfo, 981 int *in_nfavl) 982 { 983 Rt_map *nlmp, * lmp = ghp->gh_ownlmp; 984 Rt_map *clmp = slp->sl_cmap; 985 const char *name = slp->sl_name; 986 Sym *sym = 0; 987 Slookup sl = *slp; 988 989 sl.sl_flags = (LKUP_FIRST | LKUP_SPEC); 990 991 /* 992 * Continue processing a dlsym request. Lookup the required symbol in 993 * each link-map specified by the handle. 994 * 995 * To leverage off of lazy loading, dlsym() requests can result in two 996 * passes. The first descends the link-maps of any objects already in 997 * the address space. If the symbol isn't located, and lazy 998 * dependencies still exist, then a second pass is made to load these 999 * dependencies if applicable. This model means that in the case where 1000 * a symbols exists in more than one object, the one located may not be 1001 * constant - this is the standard issue with lazy loading. In addition, 1002 * attempting to locate a symbol that doesn't exist will result in the 1003 * loading of all lazy dependencies on the given handle, which can 1004 * defeat some of the advantages of lazy loading (look out JVM). 1005 */ 1006 if (ghp->gh_flags & GPH_ZERO) { 1007 Lm_list *lml; 1008 1009 /* 1010 * If this symbol lookup is triggered from a dlopen(0) handle, 1011 * traverse the present link-map list looking for promiscuous 1012 * entries. 1013 */ 1014 for (nlmp = lmp; nlmp; nlmp = NEXT_RT_MAP(nlmp)) { 1015 1016 /* 1017 * If this handle indicates we're only to look in the 1018 * first object check whether we're done. 1019 */ 1020 if ((nlmp != lmp) && (ghp->gh_flags & GPH_FIRST)) 1021 return (NULL); 1022 1023 if (!(MODE(nlmp) & RTLD_GLOBAL)) 1024 continue; 1025 if ((FLAGS(nlmp) & FLG_RT_DELETE) && 1026 ((FLAGS(clmp) & FLG_RT_DELETE) == 0)) 1027 continue; 1028 1029 sl.sl_imap = nlmp; 1030 if (sym = LM_LOOKUP_SYM(clmp)(&sl, _lmp, binfo, 1031 in_nfavl)) 1032 return (sym); 1033 } 1034 1035 /* 1036 * If we're unable to locate the symbol and this link-map still 1037 * has pending lazy dependencies, start loading them in an 1038 * attempt to exhaust the search. Note that as we're already 1039 * traversing a dynamic linked list of link-maps there's no 1040 * need for elf_lazy_find_sym() to descend the link-maps itself. 1041 */ 1042 lml = LIST(lmp); 1043 if ((lml->lm_lazy) && 1044 ((lml->lm_flags & LML_FLG_NOPENDGLBLAZY) == 0)) { 1045 int lazy = 0; 1046 1047 DBG_CALL(Dbg_syms_lazy_rescan(lml, name)); 1048 1049 sl.sl_flags |= LKUP_NODESCENT; 1050 1051 for (nlmp = lmp; nlmp; nlmp = NEXT_RT_MAP(nlmp)) { 1052 1053 if (!(MODE(nlmp) & RTLD_GLOBAL) || !LAZY(nlmp)) 1054 continue; 1055 if ((FLAGS(nlmp) & FLG_RT_DELETE) && 1056 ((FLAGS(clmp) & FLG_RT_DELETE) == 0)) 1057 continue; 1058 1059 lazy = 1; 1060 sl.sl_imap = nlmp; 1061 if (sym = elf_lazy_find_sym(&sl, _lmp, binfo, 1062 in_nfavl)) 1063 return (sym); 1064 } 1065 1066 /* 1067 * If no global, lazy loadable dependencies are found, 1068 * then none exist for this link-map list. Pending lazy 1069 * loadable objects may still exist for non-local 1070 * objects that are associated with this link-map list, 1071 * which is why we entered this fallback. Tag this 1072 * link-map list to prevent further searching for lazy 1073 * dependencies. 1074 */ 1075 if (lazy == 0) 1076 lml->lm_flags |= LML_FLG_NOPENDGLBLAZY; 1077 } 1078 } else { 1079 /* 1080 * Traverse the dlopen() handle for the presently loaded 1081 * link-maps. 1082 */ 1083 Grp_desc *gdp; 1084 Aliste idx; 1085 1086 for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) { 1087 if ((gdp->gd_flags & GPD_DLSYM) == 0) 1088 continue; 1089 1090 sl.sl_imap = gdp->gd_depend; 1091 if (sym = LM_LOOKUP_SYM(clmp)(&sl, _lmp, binfo, 1092 in_nfavl)) 1093 return (sym); 1094 1095 if (ghp->gh_flags & GPH_FIRST) 1096 return (NULL); 1097 } 1098 1099 /* 1100 * If we're unable to locate the symbol and this link-map still 1101 * has pending lazy dependencies, start loading them in an 1102 * attempt to exhaust the search. 1103 */ 1104 if ((LIST(lmp)->lm_lazy) && 1105 ((ghp->gh_flags & GPH_NOPENDLAZY) == 0)) { 1106 int lazy = 0; 1107 1108 DBG_CALL(Dbg_syms_lazy_rescan(LIST(lmp), name)); 1109 1110 for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) { 1111 nlmp = gdp->gd_depend; 1112 1113 if (((gdp->gd_flags & GPD_DLSYM) == 0) || 1114 (LAZY(nlmp) == 0)) 1115 continue; 1116 1117 lazy = 1; 1118 sl.sl_imap = nlmp; 1119 if (sym = elf_lazy_find_sym(&sl, _lmp, 1120 binfo, in_nfavl)) 1121 return (sym); 1122 } 1123 1124 /* 1125 * If no lazy loadable dependencies are found, then 1126 * none exist for this handle. Pending lazy loadable 1127 * objects may still exist for the associated link-map 1128 * list, which is why we entered this fallback. Tag 1129 * this handle to prevent further searching for lazy 1130 * dependencies. 1131 */ 1132 if (lazy == 0) 1133 ghp->gh_flags |= GPH_NOPENDLAZY; 1134 } 1135 } 1136 return (NULL); 1137 } 1138 1139 /* 1140 * Core dlsym activity. Selects symbol lookup method from handle. 1141 */ 1142 void * 1143 dlsym_core(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp, 1144 int *in_nfavl) 1145 { 1146 Sym *sym = NULL; 1147 Syminfo *sip; 1148 Slookup sl; 1149 uint_t binfo; 1150 1151 /* 1152 * Initialize the symbol lookup data structure. 1153 * 1154 * Standard relocations are evaluated using the symbol index of the 1155 * associated relocation symbol. This index provides for loading 1156 * any lazy dependency and establishing a direct binding if necessary. 1157 * If a dlsym() operation originates from an object that contains a 1158 * symbol table entry for the same name, then we need to establish the 1159 * symbol index so that any dependency requirements can be triggered. 1160 * 1161 * Therefore, the first symbol lookup that is carried out is for the 1162 * symbol name within the calling object. If this symbol exists, the 1163 * symbols index is computed, added to the Slookup data, and thus used 1164 * to seed the real symbol lookup. 1165 */ 1166 SLOOKUP_INIT(sl, name, clmp, clmp, ld_entry_cnt, elf_hash(name), 1167 0, 0, 0, LKUP_SYMNDX); 1168 1169 if (THIS_IS_ELF(clmp) && 1170 ((sym = SYMINTP(clmp)(&sl, 0, 0, NULL)) != NULL)) { 1171 sl.sl_rsymndx = (((ulong_t)sym - 1172 (ulong_t)SYMTAB(clmp)) / SYMENT(clmp)); 1173 sl.sl_rsym = sym; 1174 } 1175 1176 if (sym && (ELF_ST_VISIBILITY(sym->st_other) == STV_SINGLETON)) { 1177 Rt_map *hlmp = LIST(clmp)->lm_head; 1178 1179 /* 1180 * If a symbol reference is known, and that reference indicates 1181 * that the symbol is a singleton, then the search for the 1182 * symbol must follow the default search path. 1183 */ 1184 DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl, 0, 1185 DBG_DLSYM_SINGLETON)); 1186 1187 sl.sl_imap = hlmp; 1188 sl.sl_flags = LKUP_SPEC; 1189 if (handle == RTLD_PROBE) 1190 sl.sl_flags |= LKUP_NOFALLBACK; 1191 sym = LM_LOOKUP_SYM(clmp)(&sl, dlmp, &binfo, in_nfavl); 1192 1193 } else if (handle == RTLD_NEXT) { 1194 Rt_map *nlmp; 1195 1196 /* 1197 * If this handle is RTLD_NEXT determine whether a lazy load 1198 * from the caller might provide the next object. This mimics 1199 * the lazy loading initialization normally carried out by 1200 * lookup_sym(), however here, we must do this up-front, as 1201 * lookup_sym() will be used to inspect the next object. 1202 */ 1203 if ((sl.sl_rsymndx) && ((sip = SYMINFO(clmp)) != 0)) { 1204 /* LINTED */ 1205 sip = (Syminfo *)((char *)sip + 1206 (sl.sl_rsymndx * SYMINENT(clmp))); 1207 1208 if ((sip->si_flags & SYMINFO_FLG_DIRECT) && 1209 (sip->si_boundto < SYMINFO_BT_LOWRESERVE)) 1210 (void) elf_lazy_load(clmp, &sl, 1211 sip->si_boundto, name, in_nfavl); 1212 1213 /* 1214 * Clear the symbol index, so as not to confuse 1215 * lookup_sym() of the next object. 1216 */ 1217 sl.sl_rsymndx = 0; 1218 sl.sl_rsym = 0; 1219 } 1220 1221 /* 1222 * If the handle is RTLD_NEXT start searching in the next link 1223 * map from the callers. Determine permissions from the 1224 * present link map. Indicate to lookup_sym() that we're on an 1225 * RTLD_NEXT request so that it will use the callers link map to 1226 * start any possible lazy dependency loading. 1227 */ 1228 sl.sl_imap = nlmp = NEXT_RT_MAP(clmp); 1229 1230 DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl, 1231 (nlmp ? NAME(nlmp) : MSG_INTL(MSG_STR_NULL)), 1232 DBG_DLSYM_NEXT)); 1233 1234 if (nlmp == 0) 1235 return (0); 1236 1237 sl.sl_flags = LKUP_NEXT; 1238 sym = LM_LOOKUP_SYM(clmp)(&sl, dlmp, &binfo, in_nfavl); 1239 1240 } else if (handle == RTLD_SELF) { 1241 /* 1242 * If the handle is RTLD_SELF start searching from the caller. 1243 */ 1244 DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl, NAME(clmp), 1245 DBG_DLSYM_SELF)); 1246 1247 sl.sl_imap = clmp; 1248 sl.sl_flags = (LKUP_SPEC | LKUP_SELF); 1249 sym = LM_LOOKUP_SYM(clmp)(&sl, dlmp, &binfo, in_nfavl); 1250 1251 } else if (handle == RTLD_DEFAULT) { 1252 Rt_map *hlmp = LIST(clmp)->lm_head; 1253 1254 /* 1255 * If the handle is RTLD_DEFAULT mimic the standard symbol 1256 * lookup as would be triggered by a relocation. 1257 */ 1258 DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl, 0, 1259 DBG_DLSYM_DEFAULT)); 1260 1261 sl.sl_imap = hlmp; 1262 sl.sl_flags = LKUP_SPEC; 1263 sym = LM_LOOKUP_SYM(clmp)(&sl, dlmp, &binfo, in_nfavl); 1264 1265 } else if (handle == RTLD_PROBE) { 1266 Rt_map *hlmp = LIST(clmp)->lm_head; 1267 1268 /* 1269 * If the handle is RTLD_PROBE, mimic the standard symbol 1270 * lookup as would be triggered by a relocation, however do 1271 * not fall back to a lazy loading rescan if the symbol can't be 1272 * found within the currently loaded objects. Note, a lazy 1273 * loaded dependency required by the caller might still get 1274 * loaded to satisfy this request, but no exhaustive lazy load 1275 * rescan is carried out. 1276 */ 1277 DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl, 0, 1278 DBG_DLSYM_PROBE)); 1279 1280 sl.sl_imap = hlmp; 1281 sl.sl_flags = (LKUP_SPEC | LKUP_NOFALLBACK); 1282 sym = LM_LOOKUP_SYM(clmp)(&sl, dlmp, &binfo, in_nfavl); 1283 1284 } else { 1285 Grp_hdl *ghp = (Grp_hdl *)handle; 1286 1287 /* 1288 * Look in the shared object specified by the handle and in all 1289 * of its dependencies. 1290 */ 1291 DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl, 1292 NAME(ghp->gh_ownlmp), DBG_DLSYM_DEF)); 1293 1294 sym = LM_DLSYM(clmp)(ghp, &sl, dlmp, &binfo, in_nfavl); 1295 } 1296 1297 if (sym) { 1298 Lm_list *lml = LIST(clmp); 1299 Addr addr = sym->st_value; 1300 1301 if (!(FLAGS(*dlmp) & FLG_RT_FIXED)) 1302 addr += ADDR(*dlmp); 1303 1304 /* 1305 * Indicate that the defining object is now used. 1306 */ 1307 if (*dlmp != clmp) 1308 FLAGS1(*dlmp) |= FL1_RT_USED; 1309 1310 DBG_CALL(Dbg_bind_global(clmp, 0, 0, (Xword)-1, PLT_T_NONE, 1311 *dlmp, addr, sym->st_value, name, binfo)); 1312 1313 if ((lml->lm_tflags | AFLAGS(clmp)) & LML_TFLG_AUD_SYMBIND) { 1314 uint_t sb_flags = LA_SYMB_DLSYM; 1315 /* LINTED */ 1316 uint_t symndx = (uint_t)(((Xword)sym - 1317 (Xword)SYMTAB(*dlmp)) / SYMENT(*dlmp)); 1318 addr = audit_symbind(clmp, *dlmp, sym, symndx, addr, 1319 &sb_flags); 1320 } 1321 return ((void *)addr); 1322 } else 1323 return (0); 1324 } 1325 1326 /* 1327 * Internal dlsym activity. Called from user level or directly for internal 1328 * symbol lookup. 1329 */ 1330 void * 1331 dlsym_intn(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp) 1332 { 1333 Rt_map *llmp = 0; 1334 void *error; 1335 Aliste idx; 1336 Grp_desc *gdp; 1337 int in_nfavl = 0; 1338 1339 /* 1340 * While looking for symbols it's quite possible that additional objects 1341 * get loaded from lazy loading. These objects will have been added to 1342 * the same link-map list as those objects on the handle. Remember this 1343 * list for later investigation. 1344 */ 1345 if ((handle == RTLD_NEXT) || (handle == RTLD_DEFAULT) || 1346 (handle == RTLD_SELF) || (handle == RTLD_PROBE)) 1347 llmp = LIST(clmp)->lm_tail; 1348 else { 1349 Grp_hdl *ghp = (Grp_hdl *)handle; 1350 1351 if (ghp->gh_ownlmp) 1352 llmp = LIST(ghp->gh_ownlmp)->lm_tail; 1353 else { 1354 for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) { 1355 if ((llmp = LIST(gdp->gd_depend)->lm_tail) != 0) 1356 break; 1357 } 1358 } 1359 } 1360 1361 error = dlsym_core(handle, name, clmp, dlmp, &in_nfavl); 1362 1363 /* 1364 * If the symbol could not be found it is possible that the "not-found" 1365 * AVL tree had indicated that a required file does not exist. In case 1366 * the file system has changed since this "not-found" recording was 1367 * made, retry the dlsym() with a clean "not-found" AVL tree. 1368 */ 1369 if ((error == 0) && in_nfavl) { 1370 avl_tree_t *oavlt = nfavl; 1371 1372 nfavl = NULL; 1373 error = dlsym_core(handle, name, clmp, dlmp, NULL); 1374 1375 /* 1376 * If the symbol is found, then any file that was loaded will 1377 * have had its full path name registered in the FullPath AVL 1378 * tree. Remove any new "not-found" AVL information, and 1379 * restore the former AVL tree. 1380 */ 1381 nfavl_remove(nfavl); 1382 nfavl = oavlt; 1383 } 1384 1385 if (error == 0) { 1386 /* 1387 * Cache the error message, as Java tends to fall through this 1388 * code many times. 1389 */ 1390 if (nosym_str == 0) 1391 nosym_str = MSG_INTL(MSG_GEN_NOSYM); 1392 eprintf(LIST(clmp), ERR_FATAL, nosym_str, name); 1393 } 1394 1395 load_completion(llmp); 1396 return (error); 1397 } 1398 1399 /* 1400 * Argument checking for dlsym. Only called via external entry. 1401 */ 1402 static void * 1403 dlsym_check(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp) 1404 { 1405 /* 1406 * Verify the arguments. 1407 */ 1408 if (name == 0) { 1409 eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_ILLSYM)); 1410 return (0); 1411 } 1412 if ((handle != RTLD_NEXT) && (handle != RTLD_DEFAULT) && 1413 (handle != RTLD_SELF) && (handle != RTLD_PROBE) && 1414 (hdl_validate((Grp_hdl *)handle) == 0)) { 1415 eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL)); 1416 return (0); 1417 } 1418 return (dlsym_intn(handle, name, clmp, dlmp)); 1419 } 1420 1421 1422 #pragma weak _dlsym = dlsym 1423 1424 /* 1425 * External entry for dlsym(). On success, returns the address of the specified 1426 * symbol. On error returns a null. 1427 */ 1428 void * 1429 dlsym(void *handle, const char *name) 1430 { 1431 int entry; 1432 Rt_map *clmp, *dlmp = 0; 1433 void *addr; 1434 1435 entry = enter(0); 1436 1437 clmp = _caller(caller(), CL_EXECDEF); 1438 1439 addr = dlsym_check(handle, name, clmp, &dlmp); 1440 1441 if (entry) { 1442 if (dlmp) 1443 is_dep_init(dlmp, clmp); 1444 leave(LIST(clmp), 0); 1445 } 1446 return (addr); 1447 } 1448 1449 /* 1450 * Core dladdr activity. 1451 */ 1452 static void 1453 dladdr_core(Rt_map *clmp, void *addr, Dl_info *dlip, void **info, int flags) 1454 { 1455 /* 1456 * Set up generic information and any defaults. 1457 */ 1458 dlip->dli_fname = PATHNAME(clmp); 1459 1460 dlip->dli_fbase = (void *)ADDR(clmp); 1461 dlip->dli_sname = 0; 1462 dlip->dli_saddr = 0; 1463 1464 /* 1465 * Determine the nearest symbol to this address. 1466 */ 1467 LM_DLADDR(clmp)((ulong_t)addr, clmp, dlip, info, flags); 1468 } 1469 1470 #pragma weak _dladdr = dladdr 1471 1472 /* 1473 * External entry for dladdr(3dl) and dladdr1(3dl). Returns an information 1474 * structure that reflects the symbol closest to the address specified. 1475 */ 1476 int 1477 dladdr(void *addr, Dl_info *dlip) 1478 { 1479 int entry, error; 1480 Rt_map *clmp; 1481 1482 entry = enter(0); 1483 1484 /* 1485 * Use our calling technique to determine what object is associated 1486 * with the supplied address. If a caller can't be determined, 1487 * indicate the failure. 1488 */ 1489 if ((clmp = _caller(addr, CL_NONE)) == 0) { 1490 eprintf(0, ERR_FATAL, MSG_INTL(MSG_ARG_INVADDR), 1491 EC_NATPTR(addr)); 1492 error = 0; 1493 } else { 1494 dladdr_core(clmp, addr, dlip, 0, 0); 1495 error = 1; 1496 } 1497 1498 if (entry) 1499 leave(0, 0); 1500 return (error); 1501 } 1502 1503 #pragma weak _dladdr1 = dladdr1 1504 1505 int 1506 dladdr1(void *addr, Dl_info *dlip, void **info, int flags) 1507 { 1508 int entry, error = 0; 1509 Rt_map *clmp; 1510 1511 /* 1512 * Validate any flags. 1513 */ 1514 if (flags) { 1515 int request; 1516 1517 if (((request = (flags & RTLD_DL_MASK)) != RTLD_DL_SYMENT) && 1518 (request != RTLD_DL_LINKMAP)) { 1519 eprintf(0, ERR_FATAL, MSG_INTL(MSG_ARG_ILLFLAGS), 1520 flags); 1521 return (0); 1522 } 1523 if (info == 0) { 1524 eprintf(0, ERR_FATAL, MSG_INTL(MSG_ARG_ILLINFO), flags); 1525 return (0); 1526 } 1527 } 1528 1529 entry = enter(0); 1530 1531 /* 1532 * Use our calling technique to determine what object is associated 1533 * with the supplied address. If a caller can't be determined, 1534 * indicate the failure. 1535 */ 1536 if ((clmp = _caller(addr, CL_NONE)) == 0) { 1537 eprintf(0, ERR_FATAL, MSG_INTL(MSG_ARG_INVADDR), 1538 EC_NATPTR(addr)); 1539 error = 0; 1540 } else { 1541 dladdr_core(clmp, addr, dlip, info, flags); 1542 error = 1; 1543 } 1544 1545 if (entry) 1546 leave(0, 0); 1547 return (error); 1548 } 1549 1550 /* 1551 * Core dldump activity. 1552 */ 1553 static int 1554 dldump_core(Lm_list *lml, const char *ipath, const char *opath, int flags) 1555 { 1556 Addr addr = 0; 1557 Rt_map *lmp; 1558 1559 /* 1560 * Verify any arguments first. 1561 */ 1562 if ((!opath || (*opath == '\0')) || (ipath && (*ipath == '\0'))) { 1563 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLPATH)); 1564 return (1); 1565 } 1566 1567 /* 1568 * If an input file is specified make sure its one of our dependencies 1569 * on the main link-map list. Note, this has really all evolved for 1570 * crle(), which uses libcrle.so on an alternative link-map to trigger 1571 * dumping objects from the main link-map list. If we ever want to 1572 * dump objects from alternative link-maps, this model is going to 1573 * have to be revisited. 1574 */ 1575 if (ipath) { 1576 if ((lmp = is_so_loaded(&lml_main, ipath, NULL)) == 0) { 1577 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_NOFILE), 1578 ipath); 1579 return (1); 1580 } 1581 if (FLAGS(lmp) & FLG_RT_ALTER) { 1582 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_ALTER), ipath); 1583 return (1); 1584 } 1585 if (FLAGS(lmp) & FLG_RT_NODUMP) { 1586 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_NODUMP), 1587 ipath); 1588 return (1); 1589 } 1590 } else 1591 lmp = lml_main.lm_head; 1592 1593 1594 DBG_CALL(Dbg_file_dldump(lmp, opath, flags)); 1595 1596 /* 1597 * If the object being dump'ed isn't fixed identify its mapping. 1598 */ 1599 if (!(FLAGS(lmp) & FLG_RT_FIXED)) 1600 addr = ADDR(lmp); 1601 1602 /* 1603 * As rt_dldump() will effectively lazy load the necessary support 1604 * libraries, make sure ld.so.1 is initialized for plt relocations. 1605 */ 1606 if (elf_rtld_load() == 0) 1607 return (0); 1608 1609 /* 1610 * Dump the required image. 1611 */ 1612 return (rt_dldump(lmp, opath, flags, addr)); 1613 } 1614 1615 #pragma weak _dldump = dldump 1616 1617 /* 1618 * External entry for dldump(3c). Returns 0 on success, non-zero otherwise. 1619 */ 1620 int 1621 dldump(const char *ipath, const char *opath, int flags) 1622 { 1623 int error, entry; 1624 Rt_map *clmp; 1625 1626 entry = enter(0); 1627 1628 clmp = _caller(caller(), CL_EXECDEF); 1629 1630 error = dldump_core(LIST(clmp), ipath, opath, flags); 1631 1632 if (entry) 1633 leave(LIST(clmp), 0); 1634 return (error); 1635 } 1636 1637 /* 1638 * get_linkmap_id() translates Lm_list * pointers to the Link_map id as used by 1639 * the rtld_db and dlmopen() interfaces. It checks to see if the Link_map is 1640 * one of the primary ones and if so returns it's special token: 1641 * LM_ID_BASE 1642 * LM_ID_LDSO 1643 * 1644 * If it's not one of the primary link_map id's it will instead returns a 1645 * pointer to the Lm_list structure which uniquely identifies the Link_map. 1646 */ 1647 Lmid_t 1648 get_linkmap_id(Lm_list *lml) 1649 { 1650 if (lml->lm_flags & LML_FLG_BASELM) 1651 return (LM_ID_BASE); 1652 if (lml->lm_flags & LML_FLG_RTLDLM) 1653 return (LM_ID_LDSO); 1654 1655 return ((Lmid_t)lml); 1656 } 1657 1658 /* 1659 * Extract information for a dlopen() handle. 1660 */ 1661 static int 1662 dlinfo_core(void *handle, int request, void *p, Rt_map *clmp) 1663 { 1664 Lm_list *lml = LIST(clmp); 1665 Rt_map *lmp; 1666 1667 if ((request > RTLD_DI_MAX) || (p == 0)) { 1668 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLVAL)); 1669 return (-1); 1670 } 1671 1672 /* 1673 * Return configuration cache name and address. 1674 */ 1675 if (request == RTLD_DI_CONFIGADDR) { 1676 Dl_info *dlip = (Dl_info *)p; 1677 1678 if ((config->c_name == 0) || (config->c_bgn == 0) || 1679 (config->c_end == 0)) { 1680 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_NOCONFIG)); 1681 return (-1); 1682 } 1683 dlip->dli_fname = config->c_name; 1684 dlip->dli_fbase = (void *)config->c_bgn; 1685 return (0); 1686 } 1687 1688 /* 1689 * Return profiled object name (used by ldprof audit library). 1690 */ 1691 if (request == RTLD_DI_PROFILENAME) { 1692 if (profile_name == NULL) { 1693 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_NOPROFNAME)); 1694 return (-1); 1695 } 1696 1697 *(const char **)p = profile_name; 1698 return (0); 1699 } 1700 if (request == RTLD_DI_PROFILEOUT) { 1701 /* 1702 * If a profile destination directory hasn't been specified 1703 * provide a default. 1704 */ 1705 if (profile_out == NULL) 1706 profile_out = MSG_ORIG(MSG_PTH_VARTMP); 1707 1708 *(const char **)p = profile_out; 1709 return (0); 1710 } 1711 1712 /* 1713 * Obtain or establish a termination signal. 1714 */ 1715 if (request == RTLD_DI_GETSIGNAL) { 1716 *(int *)p = killsig; 1717 return (0); 1718 } 1719 1720 if (request == RTLD_DI_SETSIGNAL) { 1721 sigset_t set; 1722 int sig = *(int *)p; 1723 1724 /* 1725 * Determine whether the signal is in range. 1726 */ 1727 (void) sigfillset(&set); 1728 if (sigismember(&set, sig) != 1) { 1729 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_INVSIG), sig); 1730 return (-1); 1731 } 1732 1733 killsig = sig; 1734 return (0); 1735 } 1736 1737 /* 1738 * For any other request a link-map is required. Verify the handle. 1739 */ 1740 if (handle == RTLD_SELF) 1741 lmp = clmp; 1742 else { 1743 Grp_hdl *ghp = (Grp_hdl *)handle; 1744 1745 if (!hdl_validate(ghp)) { 1746 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL)); 1747 return (-1); 1748 } 1749 lmp = ghp->gh_ownlmp; 1750 } 1751 1752 /* 1753 * Obtain the process arguments, environment and auxv. Note, as the 1754 * environment can be modified by the user (putenv(3c)), reinitialize 1755 * the environment pointer on each request. 1756 */ 1757 if (request == RTLD_DI_ARGSINFO) { 1758 Dl_argsinfo *aip = (Dl_argsinfo *)p; 1759 Lm_list *lml = LIST(lmp); 1760 1761 *aip = argsinfo; 1762 if (lml->lm_flags & LML_FLG_ENVIRON) 1763 aip->dla_envp = *(lml->lm_environ); 1764 1765 return (0); 1766 } 1767 1768 /* 1769 * Return Lmid_t of the Link-Map list that the specified object is 1770 * loaded on. 1771 */ 1772 if (request == RTLD_DI_LMID) { 1773 *(Lmid_t *)p = get_linkmap_id(LIST(lmp)); 1774 return (0); 1775 } 1776 1777 /* 1778 * Return a pointer to the Link-Map structure associated with the 1779 * specified object. 1780 */ 1781 if (request == RTLD_DI_LINKMAP) { 1782 *(Link_map **)p = (Link_map *)lmp; 1783 return (0); 1784 } 1785 1786 /* 1787 * Return search path information, or the size of the buffer required 1788 * to store the information. 1789 */ 1790 if ((request == RTLD_DI_SERINFO) || (request == RTLD_DI_SERINFOSIZE)) { 1791 Spath_desc sd = { search_rules, NULL, 0 }; 1792 Pdesc *pdp; 1793 Dl_serinfo *info; 1794 Dl_serpath *path; 1795 char *strs; 1796 size_t size = sizeof (Dl_serinfo); 1797 uint_t cnt = 0; 1798 1799 info = (Dl_serinfo *)p; 1800 path = &info->dls_serpath[0]; 1801 strs = (char *)&info->dls_serpath[info->dls_cnt]; 1802 1803 /* 1804 * Traverse search path entries for this object. 1805 */ 1806 while ((pdp = get_next_dir(&sd, lmp, 0)) != 0) { 1807 size_t _size; 1808 1809 if (pdp->pd_pname == 0) 1810 continue; 1811 1812 /* 1813 * If configuration information exists, it's possible 1814 * this path has been identified as non-existent, if so 1815 * ignore it. 1816 */ 1817 if (pdp->pd_info) { 1818 Rtc_obj *dobj = (Rtc_obj *)pdp->pd_info; 1819 if (dobj->co_flags & RTC_OBJ_NOEXIST) 1820 continue; 1821 } 1822 1823 /* 1824 * Keep track of search path count and total info size. 1825 */ 1826 if (cnt++) 1827 size += sizeof (Dl_serpath); 1828 _size = pdp->pd_plen + 1; 1829 size += _size; 1830 1831 if (request == RTLD_DI_SERINFOSIZE) 1832 continue; 1833 1834 /* 1835 * If we're filling in search path information, confirm 1836 * there's sufficient space. 1837 */ 1838 if (size > info->dls_size) { 1839 eprintf(lml, ERR_FATAL, 1840 MSG_INTL(MSG_ARG_SERSIZE), 1841 EC_OFF(info->dls_size)); 1842 return (-1); 1843 } 1844 if (cnt > info->dls_cnt) { 1845 eprintf(lml, ERR_FATAL, 1846 MSG_INTL(MSG_ARG_SERCNT), info->dls_cnt); 1847 return (-1); 1848 } 1849 1850 /* 1851 * Append the path to the information buffer. 1852 */ 1853 (void) strcpy(strs, pdp->pd_pname); 1854 path->dls_name = strs; 1855 path->dls_flags = pdp->pd_flags; 1856 1857 strs = strs + _size; 1858 path++; 1859 } 1860 1861 /* 1862 * If we're here to size the search buffer fill it in. 1863 */ 1864 if (request == RTLD_DI_SERINFOSIZE) { 1865 info->dls_size = size; 1866 info->dls_cnt = cnt; 1867 } 1868 } 1869 1870 /* 1871 * Return the origin of the object associated with this link-map. 1872 * Basically return the dirname(1) of the objects fullpath. 1873 */ 1874 if (request == RTLD_DI_ORIGIN) { 1875 char *str = (char *)p; 1876 1877 (void) strncpy(str, ORIGNAME(lmp), DIRSZ(lmp)); 1878 str += DIRSZ(lmp); 1879 *str = '\0'; 1880 1881 return (0); 1882 } 1883 1884 return (0); 1885 } 1886 1887 #pragma weak _dlinfo = dlinfo 1888 1889 /* 1890 * External entry for dlinfo(3dl). 1891 */ 1892 int 1893 dlinfo(void *handle, int request, void *p) 1894 { 1895 int error, entry; 1896 Rt_map *clmp; 1897 1898 entry = enter(0); 1899 1900 clmp = _caller(caller(), CL_EXECDEF); 1901 1902 error = dlinfo_core(handle, request, p, clmp); 1903 1904 if (entry) 1905 leave(LIST(clmp), 0); 1906 return (error); 1907 } 1908