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 = NULL, *_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 == NULL) { 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, NULL); 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 = NULL; 466 Lm_list *olml = NULL; 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 EC_NATPTR(handle)); 511 return (1); 512 } 513 return (dlclose_intn(ghp, clmp)); 514 } 515 516 #pragma weak _dlclose = dlclose 517 518 /* 519 * External entry for dlclose(3dl). Returns 0 for success, non-zero otherwise. 520 */ 521 int 522 dlclose(void *handle) 523 { 524 int error, entry; 525 Rt_map *clmp; 526 527 entry = enter(0); 528 529 clmp = _caller(caller(), CL_EXECDEF); 530 531 error = dlclose_check(handle, clmp); 532 533 if (entry) 534 leave(LIST(clmp), 0); 535 return (error); 536 } 537 538 static uint_t lmid = 0; 539 540 /* 541 * The addition of new link-map lists is assumed to be in small quantities. 542 * Here, we assign a unique link-map id for diagnostic use. Simply update the 543 * running link-map count until we max out. 544 */ 545 int 546 newlmid(Lm_list *lml) 547 { 548 char buffer[MSG_LMID_ALT_SIZE + 12]; 549 550 if (lmid == UINT_MAX) { 551 lml->lm_lmid = UINT_MAX; 552 (void) strncpy(buffer, MSG_ORIG(MSG_LMID_MAXED), 553 MSG_LMID_ALT_SIZE + 12); 554 } else { 555 lml->lm_lmid = lmid++; 556 (void) snprintf(buffer, MSG_LMID_ALT_SIZE + 12, 557 MSG_ORIG(MSG_LMID_FMT), MSG_ORIG(MSG_LMID_ALT), 558 lml->lm_lmid); 559 } 560 if ((lml->lm_lmidstr = strdup(buffer)) == NULL) 561 return (0); 562 563 return (1); 564 } 565 566 /* 567 * Core dlopen activity. 568 */ 569 static Grp_hdl * 570 dlmopen_core(Lm_list *lml, const char *path, int mode, Rt_map *clmp, 571 uint_t flags, uint_t orig, int *in_nfavl) 572 { 573 Alist *palp = NULL; 574 Rt_map *nlmp; 575 Grp_hdl *ghp; 576 Aliste olmco, nlmco; 577 Lm_cntl *lmc; 578 579 DBG_CALL(Dbg_file_dlopen(clmp, 580 (path ? path : MSG_ORIG(MSG_STR_ZERO)), in_nfavl, mode)); 581 582 /* 583 * Having diagnosed the originally defined modes, assign any defaults 584 * or corrections. 585 */ 586 if (((mode & (RTLD_GROUP | RTLD_WORLD)) == 0) && 587 ((mode & RTLD_NOLOAD) == 0)) 588 mode |= (RTLD_GROUP | RTLD_WORLD); 589 if ((mode & RTLD_NOW) && (rtld_flags2 & RT_FL2_BINDLAZY)) { 590 mode &= ~RTLD_NOW; 591 mode |= RTLD_LAZY; 592 } 593 594 /* 595 * If the path specified is null then we're operating on global 596 * objects. Associate a dummy handle with the link-map list. 597 */ 598 if (path == NULL) { 599 Grp_hdl *ghp; 600 uint_t hflags = GPH_ZERO, cdflags = GPD_PARENT; 601 int promote = 0; 602 603 /* 604 * Establish any flags for the handle (Grp_hdl). 605 * 606 * . This is a dummy handle (0) that provides for a dynamic 607 * search of all global objects within the process. 608 * 609 * . Use of the RTLD_FIRST flag indicates that only the first 610 * dependency on the handle (the new object) can be used 611 * to satisfy dlsym() requests. 612 */ 613 if (mode & RTLD_FIRST) 614 hflags |= GPH_FIRST; 615 616 /* 617 * Establish the flags for this callers dependency descriptor 618 * (Grp_desc). 619 * 620 * . The explicit creation of a handle creates a descriptor 621 * for the new object and the parent (caller), 622 * 623 * . Use of the RTLD_PARENT flag indicates that the parent 624 * can be relocated against. 625 */ 626 if (mode & RTLD_PARENT) 627 cdflags |= GPD_RELOC; 628 629 if ((ghp = hdl_create(lml, 0, clmp, hflags, 630 (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS), cdflags)) == NULL) 631 return (NULL); 632 633 /* 634 * Traverse the main link-map control list, updating the mode 635 * of any objects as necessary. Call the relocation engine if 636 * this mode promotes the existing state of any relocations. 637 * crle()'s first pass loads all objects necessary for building 638 * a configuration file, however none of them are relocated. 639 * crle()'s second pass relocates objects in preparation for 640 * dldump()'ing using dlopen(0, RTLD_NOW). 641 */ 642 if ((mode & (RTLD_NOW | RTLD_CONFGEN)) == RTLD_CONFGEN) 643 return (ghp); 644 645 for (nlmp = lml->lm_head; nlmp; nlmp = NEXT_RT_MAP(nlmp)) { 646 if (((MODE(nlmp) & RTLD_GLOBAL) == 0) || 647 (FLAGS(nlmp) & FLG_RT_DELETE)) 648 continue; 649 650 if (update_mode(nlmp, MODE(nlmp), mode)) 651 promote = 1; 652 } 653 if (promote) 654 (void) relocate_lmc(lml, ALIST_OFF_DATA, clmp, 655 lml->lm_head, in_nfavl); 656 657 return (ghp); 658 } 659 660 /* 661 * Fix the pathname. If this object expands to multiple paths (ie. 662 * $ISALIST or $HWCAP have been used), then make sure the user has also 663 * furnished the RTLD_FIRST flag. As yet, we don't support opening 664 * more than one object at a time, so enforcing the RTLD_FIRST flag 665 * provides flexibility should we be able to support dlopening more 666 * than one object in the future. 667 */ 668 if (LM_FIX_NAME(clmp)(path, clmp, &palp, AL_CNT_NEEDED, orig) == NULL) 669 return (NULL); 670 671 if ((palp->al_arritems > 1) && ((mode & RTLD_FIRST) == 0)) { 672 remove_plist(&palp, 1); 673 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_5)); 674 return (NULL); 675 } 676 677 /* 678 * Create a new link-map control list for this request, and load the 679 * associated object. 680 */ 681 if ((lmc = alist_append(&lml->lm_lists, 0, sizeof (Lm_cntl), 682 AL_CNT_LMLISTS)) == NULL) { 683 remove_plist(&palp, 1); 684 return (NULL); 685 } 686 olmco = nlmco = (Aliste)((char *)lmc - (char *)lml->lm_lists); 687 688 nlmp = load_one(lml, nlmco, palp, clmp, mode, (flags | FLG_RT_HANDLE), 689 &ghp, in_nfavl); 690 691 /* 692 * Remove any expanded pathname infrastructure, and if the dependency 693 * couldn't be loaded, cleanup. 694 */ 695 remove_plist(&palp, 1); 696 if (nlmp == NULL) { 697 remove_cntl(lml, olmco); 698 return (NULL); 699 } 700 701 /* 702 * If loading an auditor was requested, and the auditor already existed, 703 * then the link-map returned will be to the original auditor. The new 704 * link-map list that was initially created, and the associated link-map 705 * control list are no longer needed. As the auditor is already loaded, 706 * we're probably done, but fall through in case additional relocations 707 * would be triggered by the mode of the caller. 708 */ 709 if ((flags & FLG_RT_AUDIT) && (LIST(nlmp) != lml)) { 710 remove_cntl(lml, olmco); 711 lml = LIST(nlmp); 712 olmco = 0; 713 nlmco = ALIST_OFF_DATA; 714 } 715 716 /* 717 * Finish processing the objects associated with this request. 718 */ 719 if (((nlmp = analyze_lmc(lml, nlmco, nlmp, in_nfavl)) == NULL) || 720 (relocate_lmc(lml, nlmco, clmp, nlmp, in_nfavl) == 0)) { 721 ghp = NULL; 722 nlmp = NULL; 723 } 724 725 /* 726 * If this lazyload has failed, and we've created a new link-map 727 * control list to which this request has added objects, then remove 728 * all the objects that have been associated to this request. 729 */ 730 if ((nlmp == NULL) && olmco && lmc->lc_head) 731 remove_lmc(lml, clmp, lmc, olmco, path); 732 733 /* 734 * Finally, remove any link-map control list that was created. 735 */ 736 if (olmco) 737 remove_cntl(lml, olmco); 738 739 return (ghp); 740 } 741 742 /* 743 * dlopen() and dlsym() operations are the means by which a process can 744 * test for the existence of required dependencies. If the necessary 745 * dependencies don't exist, then associated functionality can't be used. 746 * However, the lack of dependencies can be fixed, and the dlopen() and 747 * dlsym() requests can be repeated. As we use a "not-found" AVL tree to 748 * cache any failed full path loads, secondary dlopen() and dlsym() requests 749 * will fail, even if the dependencies have been installed. 750 * 751 * dlopen() and dlsym() retry any failures by removing the "not-found" AVL 752 * tree. Should any dependencies be found, their names are added to the 753 * FullPath AVL tree. This routine removes any new "not-found" AVL tree, 754 * so that the dlopen() or dlsym() can replace the original "not-found" tree. 755 */ 756 inline static void 757 nfavl_remove(avl_tree_t *avlt) 758 { 759 PathNode *pnp; 760 void *cookie = NULL; 761 762 if (avlt) { 763 while ((pnp = avl_destroy_nodes(avlt, &cookie)) != NULL) 764 free(pnp); 765 766 avl_destroy(avlt); 767 free(avlt); 768 } 769 } 770 771 /* 772 * Internal dlopen() activity. Called from user level or directly for internal 773 * opens that require a handle. 774 */ 775 Grp_hdl * 776 dlmopen_intn(Lm_list *lml, const char *path, int mode, Rt_map *clmp, 777 uint_t flags, uint_t orig) 778 { 779 Rt_map *dlmp = NULL; 780 Grp_hdl *ghp; 781 int in_nfavl = 0; 782 783 /* 784 * Check for magic link-map list values: 785 * 786 * LM_ID_BASE: Operate on the PRIMARY (executables) link map 787 * LM_ID_LDSO: Operation on ld.so.1's link map 788 * LM_ID_NEWLM: Create a new link-map. 789 */ 790 if (lml == (Lm_list *)LM_ID_NEWLM) { 791 if ((lml = calloc(sizeof (Lm_list), 1)) == NULL) 792 return (NULL); 793 794 /* 795 * Establish the new link-map flags from the callers and those 796 * explicitly provided. 797 */ 798 lml->lm_tflags = LIST(clmp)->lm_tflags; 799 if (flags & FLG_RT_AUDIT) { 800 /* 801 * Unset any auditing flags - an auditor shouldn't be 802 * audited. Insure all audit dependencies are loaded. 803 */ 804 lml->lm_tflags &= ~LML_TFLG_AUD_MASK; 805 lml->lm_tflags |= 806 (LML_TFLG_NOLAZYLD | LML_TFLG_LOADFLTR); 807 lml->lm_flags |= LML_FLG_NOAUDIT; 808 } 809 810 if (list_append(&dynlm_list, lml) == NULL) { 811 free(lml); 812 return (NULL); 813 } 814 if (newlmid(lml) == 0) { 815 list_delete(&dynlm_list, lml); 816 free(lml); 817 return (NULL); 818 } 819 } else if ((uintptr_t)lml < LM_ID_NUM) { 820 if ((uintptr_t)lml == LM_ID_BASE) 821 lml = &lml_main; 822 else if ((uintptr_t)lml == LM_ID_LDSO) 823 lml = &lml_rtld; 824 } 825 826 /* 827 * Open the required object on the associated link-map list. 828 */ 829 ghp = dlmopen_core(lml, path, mode, clmp, flags, orig, &in_nfavl); 830 831 /* 832 * If the object could not be found it is possible that the "not-found" 833 * AVL tree had indicated that the file does not exist. In case the 834 * file system has changed since this "not-found" recording was made, 835 * retry the dlopen() with a clean "not-found" AVL tree. 836 */ 837 if ((ghp == NULL) && in_nfavl) { 838 avl_tree_t *oavlt = nfavl; 839 840 nfavl = NULL; 841 ghp = dlmopen_core(lml, path, mode, clmp, flags, orig, NULL); 842 843 /* 844 * If the file is found, then its full path name will have been 845 * registered in the FullPath AVL tree. Remove any new 846 * "not-found" AVL information, and restore the former AVL tree. 847 */ 848 nfavl_remove(nfavl); 849 nfavl = oavlt; 850 } 851 852 /* 853 * Establish the new link-map from which .init processing will begin. 854 * Ignore .init firing when constructing a configuration file (crle(1)). 855 */ 856 if (ghp && ((mode & RTLD_CONFGEN) == 0)) 857 dlmp = ghp->gh_ownlmp; 858 859 /* 860 * If loading an auditor was requested, and the auditor already existed, 861 * then the link-map returned will be to the original auditor. Remove 862 * the link-map control list that was created for this request. 863 */ 864 if (dlmp && (flags & FLG_RT_AUDIT) && (LIST(dlmp) != lml)) { 865 remove_lml(lml); 866 lml = LIST(dlmp); 867 } 868 869 /* 870 * If this load failed, remove any alternative link-map list. 871 */ 872 if ((ghp == NULL) && 873 ((lml->lm_flags & (LML_FLG_BASELM | LML_FLG_RTLDLM)) == 0)) { 874 remove_lml(lml); 875 lml = NULL; 876 } 877 878 /* 879 * Finish this load request. If objects were loaded, .init processing 880 * is computed. Finally, the debuggers are informed of the link-map 881 * lists being stable. 882 */ 883 load_completion(dlmp); 884 885 return (ghp); 886 } 887 888 /* 889 * Argument checking for dlopen. Only called via external entry. 890 */ 891 static Grp_hdl * 892 dlmopen_check(Lm_list *lml, const char *path, int mode, Rt_map *clmp) 893 { 894 /* 895 * Verify that a valid pathname has been supplied. 896 */ 897 if (path && (*path == '\0')) { 898 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLPATH)); 899 return (0); 900 } 901 902 /* 903 * Historically we've always verified the mode is either RTLD_NOW or 904 * RTLD_LAZY. RTLD_NOLOAD is valid by itself. Use of LM_ID_NEWLM 905 * requires a specific pathname, and use of RTLD_PARENT is meaningless. 906 */ 907 if ((mode & (RTLD_NOW | RTLD_LAZY | RTLD_NOLOAD)) == 0) { 908 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_1)); 909 return (0); 910 } 911 if ((mode & (RTLD_NOW | RTLD_LAZY)) == (RTLD_NOW | RTLD_LAZY)) { 912 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_2)); 913 return (0); 914 } 915 if ((lml == (Lm_list *)LM_ID_NEWLM) && (path == NULL)) { 916 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_3)); 917 return (0); 918 } 919 if ((lml == (Lm_list *)LM_ID_NEWLM) && (mode & RTLD_PARENT)) { 920 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_4)); 921 return (0); 922 } 923 924 return (dlmopen_intn(lml, path, mode, clmp, 0, 0)); 925 } 926 927 #pragma weak _dlopen = dlopen 928 929 /* 930 * External entry for dlopen(3dl). On success, returns a pointer (handle) to 931 * the structure containing information about the newly added object, ie. can 932 * be used by dlsym(). On failure, returns a null pointer. 933 */ 934 void * 935 dlopen(const char *path, int mode) 936 { 937 int entry; 938 Rt_map *clmp; 939 Grp_hdl *ghp; 940 Lm_list *lml; 941 942 entry = enter(0); 943 944 clmp = _caller(caller(), CL_EXECDEF); 945 lml = LIST(clmp); 946 947 ghp = dlmopen_check(lml, path, mode, clmp); 948 949 if (entry) 950 leave(lml, 0); 951 return ((void *)ghp); 952 } 953 954 #pragma weak _dlmopen = dlmopen 955 956 /* 957 * External entry for dlmopen(3dl). 958 */ 959 void * 960 dlmopen(Lmid_t lmid, const char *path, int mode) 961 { 962 int entry; 963 Rt_map *clmp; 964 Grp_hdl *ghp; 965 966 entry = enter(0); 967 968 clmp = _caller(caller(), CL_EXECDEF); 969 970 ghp = dlmopen_check((Lm_list *)lmid, path, mode, clmp); 971 972 if (entry) 973 leave(LIST(clmp), 0); 974 return ((void *)ghp); 975 } 976 977 /* 978 * Handle processing for dlsym. 979 */ 980 Sym * 981 dlsym_handle(Grp_hdl *ghp, Slookup *slp, Rt_map **_lmp, uint_t *binfo, 982 int *in_nfavl) 983 { 984 Rt_map *nlmp, * lmp = ghp->gh_ownlmp; 985 Rt_map *clmp = slp->sl_cmap; 986 const char *name = slp->sl_name; 987 Sym *sym = NULL; 988 Slookup sl = *slp; 989 990 sl.sl_flags = (LKUP_FIRST | LKUP_SPEC); 991 992 /* 993 * Continue processing a dlsym request. Lookup the required symbol in 994 * each link-map specified by the handle. 995 * 996 * To leverage off of lazy loading, dlsym() requests can result in two 997 * passes. The first descends the link-maps of any objects already in 998 * the address space. If the symbol isn't located, and lazy 999 * dependencies still exist, then a second pass is made to load these 1000 * dependencies if applicable. This model means that in the case where 1001 * a symbols exists in more than one object, the one located may not be 1002 * constant - this is the standard issue with lazy loading. In addition, 1003 * attempting to locate a symbol that doesn't exist will result in the 1004 * loading of all lazy dependencies on the given handle, which can 1005 * defeat some of the advantages of lazy loading (look out JVM). 1006 */ 1007 if (ghp->gh_flags & GPH_ZERO) { 1008 Lm_list *lml; 1009 1010 /* 1011 * If this symbol lookup is triggered from a dlopen(0) handle, 1012 * traverse the present link-map list looking for promiscuous 1013 * entries. 1014 */ 1015 for (nlmp = lmp; nlmp; nlmp = NEXT_RT_MAP(nlmp)) { 1016 1017 /* 1018 * If this handle indicates we're only to look in the 1019 * first object check whether we're done. 1020 */ 1021 if ((nlmp != lmp) && (ghp->gh_flags & GPH_FIRST)) 1022 return (NULL); 1023 1024 if (!(MODE(nlmp) & RTLD_GLOBAL)) 1025 continue; 1026 if ((FLAGS(nlmp) & FLG_RT_DELETE) && 1027 ((FLAGS(clmp) & FLG_RT_DELETE) == 0)) 1028 continue; 1029 1030 sl.sl_imap = nlmp; 1031 if (sym = LM_LOOKUP_SYM(clmp)(&sl, _lmp, binfo, 1032 in_nfavl)) 1033 return (sym); 1034 } 1035 1036 /* 1037 * If we're unable to locate the symbol and this link-map still 1038 * has pending lazy dependencies, start loading them in an 1039 * attempt to exhaust the search. Note that as we're already 1040 * traversing a dynamic linked list of link-maps there's no 1041 * need for elf_lazy_find_sym() to descend the link-maps itself. 1042 */ 1043 lml = LIST(lmp); 1044 if ((lml->lm_lazy) && 1045 ((lml->lm_flags & LML_FLG_NOPENDGLBLAZY) == 0)) { 1046 int lazy = 0; 1047 1048 DBG_CALL(Dbg_syms_lazy_rescan(lml, name)); 1049 1050 sl.sl_flags |= LKUP_NODESCENT; 1051 1052 for (nlmp = lmp; nlmp; nlmp = NEXT_RT_MAP(nlmp)) { 1053 1054 if (!(MODE(nlmp) & RTLD_GLOBAL) || !LAZY(nlmp)) 1055 continue; 1056 if ((FLAGS(nlmp) & FLG_RT_DELETE) && 1057 ((FLAGS(clmp) & FLG_RT_DELETE) == 0)) 1058 continue; 1059 1060 lazy = 1; 1061 sl.sl_imap = nlmp; 1062 if (sym = elf_lazy_find_sym(&sl, _lmp, binfo, 1063 in_nfavl)) 1064 return (sym); 1065 } 1066 1067 /* 1068 * If no global, lazy loadable dependencies are found, 1069 * then none exist for this link-map list. Pending lazy 1070 * loadable objects may still exist for non-local 1071 * objects that are associated with this link-map list, 1072 * which is why we entered this fallback. Tag this 1073 * link-map list to prevent further searching for lazy 1074 * dependencies. 1075 */ 1076 if (lazy == 0) 1077 lml->lm_flags |= LML_FLG_NOPENDGLBLAZY; 1078 } 1079 } else { 1080 /* 1081 * Traverse the dlopen() handle for the presently loaded 1082 * link-maps. 1083 */ 1084 Grp_desc *gdp; 1085 Aliste idx; 1086 1087 for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) { 1088 if ((gdp->gd_flags & GPD_DLSYM) == 0) 1089 continue; 1090 1091 sl.sl_imap = gdp->gd_depend; 1092 if (sym = LM_LOOKUP_SYM(clmp)(&sl, _lmp, binfo, 1093 in_nfavl)) 1094 return (sym); 1095 1096 if (ghp->gh_flags & GPH_FIRST) 1097 return (NULL); 1098 } 1099 1100 /* 1101 * If we're unable to locate the symbol and this link-map still 1102 * has pending lazy dependencies, start loading them in an 1103 * attempt to exhaust the search. 1104 */ 1105 if ((LIST(lmp)->lm_lazy) && 1106 ((ghp->gh_flags & GPH_NOPENDLAZY) == 0)) { 1107 int lazy = 0; 1108 1109 DBG_CALL(Dbg_syms_lazy_rescan(LIST(lmp), name)); 1110 1111 for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) { 1112 nlmp = gdp->gd_depend; 1113 1114 if (((gdp->gd_flags & GPD_DLSYM) == 0) || 1115 (LAZY(nlmp) == 0)) 1116 continue; 1117 1118 lazy = 1; 1119 sl.sl_imap = nlmp; 1120 if (sym = elf_lazy_find_sym(&sl, _lmp, 1121 binfo, in_nfavl)) 1122 return (sym); 1123 } 1124 1125 /* 1126 * If no lazy loadable dependencies are found, then 1127 * none exist for this handle. Pending lazy loadable 1128 * objects may still exist for the associated link-map 1129 * list, which is why we entered this fallback. Tag 1130 * this handle to prevent further searching for lazy 1131 * dependencies. 1132 */ 1133 if (lazy == 0) 1134 ghp->gh_flags |= GPH_NOPENDLAZY; 1135 } 1136 } 1137 return (NULL); 1138 } 1139 1140 /* 1141 * Core dlsym activity. Selects symbol lookup method from handle. 1142 */ 1143 void * 1144 dlsym_core(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp, 1145 int *in_nfavl) 1146 { 1147 Sym *sym = NULL; 1148 Syminfo *sip; 1149 Slookup sl; 1150 uint_t binfo; 1151 1152 /* 1153 * Initialize the symbol lookup data structure. 1154 * 1155 * Standard relocations are evaluated using the symbol index of the 1156 * associated relocation symbol. This index provides for loading 1157 * any lazy dependency and establishing a direct binding if necessary. 1158 * If a dlsym() operation originates from an object that contains a 1159 * symbol table entry for the same name, then we need to establish the 1160 * symbol index so that any dependency requirements can be triggered. 1161 * 1162 * Therefore, the first symbol lookup that is carried out is for the 1163 * symbol name within the calling object. If this symbol exists, the 1164 * symbols index is computed, added to the Slookup data, and thus used 1165 * to seed the real symbol lookup. 1166 */ 1167 SLOOKUP_INIT(sl, name, clmp, clmp, ld_entry_cnt, elf_hash(name), 1168 0, 0, 0, LKUP_SYMNDX); 1169 1170 if (THIS_IS_ELF(clmp) && 1171 ((sym = SYMINTP(clmp)(&sl, 0, 0, NULL)) != NULL)) { 1172 sl.sl_rsymndx = (((ulong_t)sym - 1173 (ulong_t)SYMTAB(clmp)) / SYMENT(clmp)); 1174 sl.sl_rsym = sym; 1175 } 1176 1177 if (sym && (ELF_ST_VISIBILITY(sym->st_other) == STV_SINGLETON)) { 1178 Rt_map *hlmp = LIST(clmp)->lm_head; 1179 1180 /* 1181 * If a symbol reference is known, and that reference indicates 1182 * that the symbol is a singleton, then the search for the 1183 * symbol must follow the default search path. 1184 */ 1185 DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl, 0, 1186 DBG_DLSYM_SINGLETON)); 1187 1188 sl.sl_imap = hlmp; 1189 sl.sl_flags = LKUP_SPEC; 1190 if (handle == RTLD_PROBE) 1191 sl.sl_flags |= LKUP_NOFALLBACK; 1192 sym = LM_LOOKUP_SYM(clmp)(&sl, dlmp, &binfo, in_nfavl); 1193 1194 } else if (handle == RTLD_NEXT) { 1195 Rt_map *nlmp; 1196 1197 /* 1198 * If this handle is RTLD_NEXT determine whether a lazy load 1199 * from the caller might provide the next object. This mimics 1200 * the lazy loading initialization normally carried out by 1201 * lookup_sym(), however here, we must do this up-front, as 1202 * lookup_sym() will be used to inspect the next object. 1203 */ 1204 if ((sl.sl_rsymndx) && ((sip = SYMINFO(clmp)) != NULL)) { 1205 /* LINTED */ 1206 sip = (Syminfo *)((char *)sip + 1207 (sl.sl_rsymndx * SYMINENT(clmp))); 1208 1209 if ((sip->si_flags & SYMINFO_FLG_DIRECT) && 1210 (sip->si_boundto < SYMINFO_BT_LOWRESERVE)) 1211 (void) elf_lazy_load(clmp, &sl, 1212 sip->si_boundto, name, in_nfavl); 1213 1214 /* 1215 * Clear the symbol index, so as not to confuse 1216 * lookup_sym() of the next object. 1217 */ 1218 sl.sl_rsymndx = 0; 1219 sl.sl_rsym = NULL; 1220 } 1221 1222 /* 1223 * If the handle is RTLD_NEXT start searching in the next link 1224 * map from the callers. Determine permissions from the 1225 * present link map. Indicate to lookup_sym() that we're on an 1226 * RTLD_NEXT request so that it will use the callers link map to 1227 * start any possible lazy dependency loading. 1228 */ 1229 sl.sl_imap = nlmp = NEXT_RT_MAP(clmp); 1230 1231 DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl, 1232 (nlmp ? NAME(nlmp) : MSG_INTL(MSG_STR_NULL)), 1233 DBG_DLSYM_NEXT)); 1234 1235 if (nlmp == NULL) 1236 return (0); 1237 1238 sl.sl_flags = LKUP_NEXT; 1239 sym = LM_LOOKUP_SYM(clmp)(&sl, dlmp, &binfo, in_nfavl); 1240 1241 } else if (handle == RTLD_SELF) { 1242 /* 1243 * If the handle is RTLD_SELF start searching from the caller. 1244 */ 1245 DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl, NAME(clmp), 1246 DBG_DLSYM_SELF)); 1247 1248 sl.sl_imap = clmp; 1249 sl.sl_flags = (LKUP_SPEC | LKUP_SELF); 1250 sym = LM_LOOKUP_SYM(clmp)(&sl, dlmp, &binfo, in_nfavl); 1251 1252 } else if (handle == RTLD_DEFAULT) { 1253 Rt_map *hlmp = LIST(clmp)->lm_head; 1254 1255 /* 1256 * If the handle is RTLD_DEFAULT mimic the standard symbol 1257 * lookup as would be triggered by a relocation. 1258 */ 1259 DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl, 0, 1260 DBG_DLSYM_DEFAULT)); 1261 1262 sl.sl_imap = hlmp; 1263 sl.sl_flags = LKUP_SPEC; 1264 sym = LM_LOOKUP_SYM(clmp)(&sl, dlmp, &binfo, in_nfavl); 1265 1266 } else if (handle == RTLD_PROBE) { 1267 Rt_map *hlmp = LIST(clmp)->lm_head; 1268 1269 /* 1270 * If the handle is RTLD_PROBE, mimic the standard symbol 1271 * lookup as would be triggered by a relocation, however do 1272 * not fall back to a lazy loading rescan if the symbol can't be 1273 * found within the currently loaded objects. Note, a lazy 1274 * loaded dependency required by the caller might still get 1275 * loaded to satisfy this request, but no exhaustive lazy load 1276 * rescan is carried out. 1277 */ 1278 DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl, 0, 1279 DBG_DLSYM_PROBE)); 1280 1281 sl.sl_imap = hlmp; 1282 sl.sl_flags = (LKUP_SPEC | LKUP_NOFALLBACK); 1283 sym = LM_LOOKUP_SYM(clmp)(&sl, dlmp, &binfo, in_nfavl); 1284 1285 } else { 1286 Grp_hdl *ghp = (Grp_hdl *)handle; 1287 1288 /* 1289 * Look in the shared object specified by the handle and in all 1290 * of its dependencies. 1291 */ 1292 DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl, 1293 NAME(ghp->gh_ownlmp), DBG_DLSYM_DEF)); 1294 1295 sym = LM_DLSYM(clmp)(ghp, &sl, dlmp, &binfo, in_nfavl); 1296 } 1297 1298 if (sym) { 1299 Lm_list *lml = LIST(clmp); 1300 Addr addr = sym->st_value; 1301 1302 if (!(FLAGS(*dlmp) & FLG_RT_FIXED)) 1303 addr += ADDR(*dlmp); 1304 1305 /* 1306 * Indicate that the defining object is now used. 1307 */ 1308 if (*dlmp != clmp) 1309 FLAGS1(*dlmp) |= FL1_RT_USED; 1310 1311 DBG_CALL(Dbg_bind_global(clmp, 0, 0, (Xword)-1, PLT_T_NONE, 1312 *dlmp, addr, sym->st_value, name, binfo)); 1313 1314 if ((lml->lm_tflags | AFLAGS(clmp)) & LML_TFLG_AUD_SYMBIND) { 1315 uint_t sb_flags = LA_SYMB_DLSYM; 1316 /* LINTED */ 1317 uint_t symndx = (uint_t)(((Xword)sym - 1318 (Xword)SYMTAB(*dlmp)) / SYMENT(*dlmp)); 1319 addr = audit_symbind(clmp, *dlmp, sym, symndx, addr, 1320 &sb_flags); 1321 } 1322 return ((void *)addr); 1323 } else 1324 return (0); 1325 } 1326 1327 /* 1328 * Internal dlsym activity. Called from user level or directly for internal 1329 * symbol lookup. 1330 */ 1331 void * 1332 dlsym_intn(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp) 1333 { 1334 Rt_map *llmp = NULL; 1335 void *error; 1336 Aliste idx; 1337 Grp_desc *gdp; 1338 int in_nfavl = 0; 1339 1340 /* 1341 * While looking for symbols it's quite possible that additional objects 1342 * get loaded from lazy loading. These objects will have been added to 1343 * the same link-map list as those objects on the handle. Remember this 1344 * list for later investigation. 1345 */ 1346 if ((handle == RTLD_NEXT) || (handle == RTLD_DEFAULT) || 1347 (handle == RTLD_SELF) || (handle == RTLD_PROBE)) 1348 llmp = LIST(clmp)->lm_tail; 1349 else { 1350 Grp_hdl *ghp = (Grp_hdl *)handle; 1351 1352 if (ghp->gh_ownlmp) 1353 llmp = LIST(ghp->gh_ownlmp)->lm_tail; 1354 else { 1355 for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) { 1356 if ((llmp = 1357 LIST(gdp->gd_depend)->lm_tail) != NULL) 1358 break; 1359 } 1360 } 1361 } 1362 1363 error = dlsym_core(handle, name, clmp, dlmp, &in_nfavl); 1364 1365 /* 1366 * If the symbol could not be found it is possible that the "not-found" 1367 * AVL tree had indicated that a required file does not exist. In case 1368 * the file system has changed since this "not-found" recording was 1369 * made, retry the dlsym() with a clean "not-found" AVL tree. 1370 */ 1371 if ((error == NULL) && in_nfavl) { 1372 avl_tree_t *oavlt = nfavl; 1373 1374 nfavl = NULL; 1375 error = dlsym_core(handle, name, clmp, dlmp, NULL); 1376 1377 /* 1378 * If the symbol is found, then any file that was loaded will 1379 * have had its full path name registered in the FullPath AVL 1380 * tree. Remove any new "not-found" AVL information, and 1381 * restore the former AVL tree. 1382 */ 1383 nfavl_remove(nfavl); 1384 nfavl = oavlt; 1385 } 1386 1387 if (error == NULL) { 1388 /* 1389 * Cache the error message, as Java tends to fall through this 1390 * code many times. 1391 */ 1392 if (nosym_str == NULL) 1393 nosym_str = MSG_INTL(MSG_GEN_NOSYM); 1394 eprintf(LIST(clmp), ERR_FATAL, nosym_str, name); 1395 } 1396 1397 load_completion(llmp); 1398 return (error); 1399 } 1400 1401 /* 1402 * Argument checking for dlsym. Only called via external entry. 1403 */ 1404 static void * 1405 dlsym_check(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp) 1406 { 1407 /* 1408 * Verify the arguments. 1409 */ 1410 if (name == NULL) { 1411 eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_ILLSYM)); 1412 return (NULL); 1413 } 1414 if ((handle != RTLD_NEXT) && (handle != RTLD_DEFAULT) && 1415 (handle != RTLD_SELF) && (handle != RTLD_PROBE) && 1416 (hdl_validate((Grp_hdl *)handle) == 0)) { 1417 eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL), 1418 EC_NATPTR(handle)); 1419 return (NULL); 1420 } 1421 return (dlsym_intn(handle, name, clmp, dlmp)); 1422 } 1423 1424 1425 #pragma weak _dlsym = dlsym 1426 1427 /* 1428 * External entry for dlsym(). On success, returns the address of the specified 1429 * symbol. On error returns a null. 1430 */ 1431 void * 1432 dlsym(void *handle, const char *name) 1433 { 1434 int entry; 1435 Rt_map *clmp, *dlmp = NULL; 1436 void *addr; 1437 1438 entry = enter(0); 1439 1440 clmp = _caller(caller(), CL_EXECDEF); 1441 1442 addr = dlsym_check(handle, name, clmp, &dlmp); 1443 1444 if (entry) { 1445 if (dlmp) 1446 is_dep_init(dlmp, clmp); 1447 leave(LIST(clmp), 0); 1448 } 1449 return (addr); 1450 } 1451 1452 /* 1453 * Core dladdr activity. 1454 */ 1455 static void 1456 dladdr_core(Rt_map *clmp, void *addr, Dl_info *dlip, void **info, int flags) 1457 { 1458 /* 1459 * Set up generic information and any defaults. 1460 */ 1461 dlip->dli_fname = PATHNAME(clmp); 1462 1463 dlip->dli_fbase = (void *)ADDR(clmp); 1464 dlip->dli_sname = NULL; 1465 dlip->dli_saddr = NULL; 1466 1467 /* 1468 * Determine the nearest symbol to this address. 1469 */ 1470 LM_DLADDR(clmp)((ulong_t)addr, clmp, dlip, info, flags); 1471 } 1472 1473 #pragma weak _dladdr = dladdr 1474 1475 /* 1476 * External entry for dladdr(3dl) and dladdr1(3dl). Returns an information 1477 * structure that reflects the symbol closest to the address specified. 1478 */ 1479 int 1480 dladdr(void *addr, Dl_info *dlip) 1481 { 1482 int entry, error; 1483 Rt_map *clmp; 1484 1485 entry = enter(0); 1486 1487 /* 1488 * Use our calling technique to determine what object is associated 1489 * with the supplied address. If a caller can't be determined, 1490 * indicate the failure. 1491 */ 1492 if ((clmp = _caller(addr, CL_NONE)) == NULL) { 1493 eprintf(0, ERR_FATAL, MSG_INTL(MSG_ARG_INVADDR), 1494 EC_NATPTR(addr)); 1495 error = 0; 1496 } else { 1497 dladdr_core(clmp, addr, dlip, 0, 0); 1498 error = 1; 1499 } 1500 1501 if (entry) 1502 leave(0, 0); 1503 return (error); 1504 } 1505 1506 #pragma weak _dladdr1 = dladdr1 1507 1508 int 1509 dladdr1(void *addr, Dl_info *dlip, void **info, int flags) 1510 { 1511 int entry, error = 0; 1512 Rt_map *clmp; 1513 1514 /* 1515 * Validate any flags. 1516 */ 1517 if (flags) { 1518 int request; 1519 1520 if (((request = (flags & RTLD_DL_MASK)) != RTLD_DL_SYMENT) && 1521 (request != RTLD_DL_LINKMAP)) { 1522 eprintf(0, ERR_FATAL, MSG_INTL(MSG_ARG_ILLFLAGS), 1523 flags); 1524 return (0); 1525 } 1526 if (info == NULL) { 1527 eprintf(0, ERR_FATAL, MSG_INTL(MSG_ARG_ILLINFO), flags); 1528 return (0); 1529 } 1530 } 1531 1532 entry = enter(0); 1533 1534 /* 1535 * Use our calling technique to determine what object is associated 1536 * with the supplied address. If a caller can't be determined, 1537 * indicate the failure. 1538 */ 1539 if ((clmp = _caller(addr, CL_NONE)) == NULL) { 1540 eprintf(0, ERR_FATAL, MSG_INTL(MSG_ARG_INVADDR), 1541 EC_NATPTR(addr)); 1542 error = 0; 1543 } else { 1544 dladdr_core(clmp, addr, dlip, info, flags); 1545 error = 1; 1546 } 1547 1548 if (entry) 1549 leave(0, 0); 1550 return (error); 1551 } 1552 1553 /* 1554 * Core dldump activity. 1555 */ 1556 static int 1557 dldump_core(Lm_list *lml, const char *ipath, const char *opath, int flags) 1558 { 1559 Addr addr = 0; 1560 Rt_map *lmp; 1561 1562 /* 1563 * Verify any arguments first. 1564 */ 1565 if ((!opath || (*opath == '\0')) || (ipath && (*ipath == '\0'))) { 1566 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLPATH)); 1567 return (1); 1568 } 1569 1570 /* 1571 * If an input file is specified make sure its one of our dependencies 1572 * on the main link-map list. Note, this has really all evolved for 1573 * crle(), which uses libcrle.so on an alternative link-map to trigger 1574 * dumping objects from the main link-map list. If we ever want to 1575 * dump objects from alternative link-maps, this model is going to 1576 * have to be revisited. 1577 */ 1578 if (ipath) { 1579 if ((lmp = is_so_loaded(&lml_main, ipath, NULL)) == NULL) { 1580 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_NOFILE), 1581 ipath); 1582 return (1); 1583 } 1584 if (FLAGS(lmp) & FLG_RT_ALTER) { 1585 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_ALTER), ipath); 1586 return (1); 1587 } 1588 if (FLAGS(lmp) & FLG_RT_NODUMP) { 1589 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_NODUMP), 1590 ipath); 1591 return (1); 1592 } 1593 } else 1594 lmp = lml_main.lm_head; 1595 1596 1597 DBG_CALL(Dbg_file_dldump(lmp, opath, flags)); 1598 1599 /* 1600 * If the object being dump'ed isn't fixed identify its mapping. 1601 */ 1602 if (!(FLAGS(lmp) & FLG_RT_FIXED)) 1603 addr = ADDR(lmp); 1604 1605 /* 1606 * As rt_dldump() will effectively lazy load the necessary support 1607 * libraries, make sure ld.so.1 is initialized for plt relocations. 1608 */ 1609 if (elf_rtld_load() == 0) 1610 return (0); 1611 1612 /* 1613 * Dump the required image. 1614 */ 1615 return (rt_dldump(lmp, opath, flags, addr)); 1616 } 1617 1618 #pragma weak _dldump = dldump 1619 1620 /* 1621 * External entry for dldump(3c). Returns 0 on success, non-zero otherwise. 1622 */ 1623 int 1624 dldump(const char *ipath, const char *opath, int flags) 1625 { 1626 int error, entry; 1627 Rt_map *clmp; 1628 1629 entry = enter(0); 1630 1631 clmp = _caller(caller(), CL_EXECDEF); 1632 1633 error = dldump_core(LIST(clmp), ipath, opath, flags); 1634 1635 if (entry) 1636 leave(LIST(clmp), 0); 1637 return (error); 1638 } 1639 1640 /* 1641 * get_linkmap_id() translates Lm_list * pointers to the Link_map id as used by 1642 * the rtld_db and dlmopen() interfaces. It checks to see if the Link_map is 1643 * one of the primary ones and if so returns it's special token: 1644 * LM_ID_BASE 1645 * LM_ID_LDSO 1646 * 1647 * If it's not one of the primary link_map id's it will instead returns a 1648 * pointer to the Lm_list structure which uniquely identifies the Link_map. 1649 */ 1650 Lmid_t 1651 get_linkmap_id(Lm_list *lml) 1652 { 1653 if (lml->lm_flags & LML_FLG_BASELM) 1654 return (LM_ID_BASE); 1655 if (lml->lm_flags & LML_FLG_RTLDLM) 1656 return (LM_ID_LDSO); 1657 1658 return ((Lmid_t)lml); 1659 } 1660 1661 /* 1662 * Extract information for a dlopen() handle. 1663 */ 1664 static int 1665 dlinfo_core(void *handle, int request, void *p, Rt_map *clmp) 1666 { 1667 Lm_list *lml = LIST(clmp); 1668 Rt_map *lmp; 1669 1670 if ((request > RTLD_DI_MAX) || (p == NULL)) { 1671 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLVAL)); 1672 return (-1); 1673 } 1674 1675 /* 1676 * Return configuration cache name and address. 1677 */ 1678 if (request == RTLD_DI_CONFIGADDR) { 1679 Dl_info *dlip = (Dl_info *)p; 1680 1681 if ((config->c_name == NULL) || (config->c_bgn == 0) || 1682 (config->c_end == 0)) { 1683 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_NOCONFIG)); 1684 return (-1); 1685 } 1686 dlip->dli_fname = config->c_name; 1687 dlip->dli_fbase = (void *)config->c_bgn; 1688 return (0); 1689 } 1690 1691 /* 1692 * Return profiled object name (used by ldprof audit library). 1693 */ 1694 if (request == RTLD_DI_PROFILENAME) { 1695 if (profile_name == NULL) { 1696 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_NOPROFNAME)); 1697 return (-1); 1698 } 1699 1700 *(const char **)p = profile_name; 1701 return (0); 1702 } 1703 if (request == RTLD_DI_PROFILEOUT) { 1704 /* 1705 * If a profile destination directory hasn't been specified 1706 * provide a default. 1707 */ 1708 if (profile_out == NULL) 1709 profile_out = MSG_ORIG(MSG_PTH_VARTMP); 1710 1711 *(const char **)p = profile_out; 1712 return (0); 1713 } 1714 1715 /* 1716 * Obtain or establish a termination signal. 1717 */ 1718 if (request == RTLD_DI_GETSIGNAL) { 1719 *(int *)p = killsig; 1720 return (0); 1721 } 1722 1723 if (request == RTLD_DI_SETSIGNAL) { 1724 sigset_t set; 1725 int sig = *(int *)p; 1726 1727 /* 1728 * Determine whether the signal is in range. 1729 */ 1730 (void) sigfillset(&set); 1731 if (sigismember(&set, sig) != 1) { 1732 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_INVSIG), sig); 1733 return (-1); 1734 } 1735 1736 killsig = sig; 1737 return (0); 1738 } 1739 1740 /* 1741 * For any other request a link-map is required. Verify the handle. 1742 */ 1743 if (handle == RTLD_SELF) 1744 lmp = clmp; 1745 else { 1746 Grp_hdl *ghp = (Grp_hdl *)handle; 1747 1748 if (!hdl_validate(ghp)) { 1749 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL), 1750 EC_NATPTR(handle)); 1751 return (-1); 1752 } 1753 lmp = ghp->gh_ownlmp; 1754 } 1755 1756 /* 1757 * Obtain the process arguments, environment and auxv. Note, as the 1758 * environment can be modified by the user (putenv(3c)), reinitialize 1759 * the environment pointer on each request. 1760 */ 1761 if (request == RTLD_DI_ARGSINFO) { 1762 Dl_argsinfo *aip = (Dl_argsinfo *)p; 1763 Lm_list *lml = LIST(lmp); 1764 1765 *aip = argsinfo; 1766 if (lml->lm_flags & LML_FLG_ENVIRON) 1767 aip->dla_envp = *(lml->lm_environ); 1768 1769 return (0); 1770 } 1771 1772 /* 1773 * Return Lmid_t of the Link-Map list that the specified object is 1774 * loaded on. 1775 */ 1776 if (request == RTLD_DI_LMID) { 1777 *(Lmid_t *)p = get_linkmap_id(LIST(lmp)); 1778 return (0); 1779 } 1780 1781 /* 1782 * Return a pointer to the Link-Map structure associated with the 1783 * specified object. 1784 */ 1785 if (request == RTLD_DI_LINKMAP) { 1786 *(Link_map **)p = (Link_map *)lmp; 1787 return (0); 1788 } 1789 1790 /* 1791 * Return search path information, or the size of the buffer required 1792 * to store the information. 1793 */ 1794 if ((request == RTLD_DI_SERINFO) || (request == RTLD_DI_SERINFOSIZE)) { 1795 Spath_desc sd = { search_rules, NULL, 0 }; 1796 Pdesc *pdp; 1797 Dl_serinfo *info; 1798 Dl_serpath *path; 1799 char *strs; 1800 size_t size = sizeof (Dl_serinfo); 1801 uint_t cnt = 0; 1802 1803 info = (Dl_serinfo *)p; 1804 path = &info->dls_serpath[0]; 1805 strs = (char *)&info->dls_serpath[info->dls_cnt]; 1806 1807 /* 1808 * Traverse search path entries for this object. 1809 */ 1810 while ((pdp = get_next_dir(&sd, lmp, 0)) != NULL) { 1811 size_t _size; 1812 1813 if (pdp->pd_pname == NULL) 1814 continue; 1815 1816 /* 1817 * If configuration information exists, it's possible 1818 * this path has been identified as non-existent, if so 1819 * ignore it. 1820 */ 1821 if (pdp->pd_info) { 1822 Rtc_obj *dobj = (Rtc_obj *)pdp->pd_info; 1823 if (dobj->co_flags & RTC_OBJ_NOEXIST) 1824 continue; 1825 } 1826 1827 /* 1828 * Keep track of search path count and total info size. 1829 */ 1830 if (cnt++) 1831 size += sizeof (Dl_serpath); 1832 _size = pdp->pd_plen + 1; 1833 size += _size; 1834 1835 if (request == RTLD_DI_SERINFOSIZE) 1836 continue; 1837 1838 /* 1839 * If we're filling in search path information, confirm 1840 * there's sufficient space. 1841 */ 1842 if (size > info->dls_size) { 1843 eprintf(lml, ERR_FATAL, 1844 MSG_INTL(MSG_ARG_SERSIZE), 1845 EC_OFF(info->dls_size)); 1846 return (-1); 1847 } 1848 if (cnt > info->dls_cnt) { 1849 eprintf(lml, ERR_FATAL, 1850 MSG_INTL(MSG_ARG_SERCNT), info->dls_cnt); 1851 return (-1); 1852 } 1853 1854 /* 1855 * Append the path to the information buffer. 1856 */ 1857 (void) strcpy(strs, pdp->pd_pname); 1858 path->dls_name = strs; 1859 path->dls_flags = pdp->pd_flags; 1860 1861 strs = strs + _size; 1862 path++; 1863 } 1864 1865 /* 1866 * If we're here to size the search buffer fill it in. 1867 */ 1868 if (request == RTLD_DI_SERINFOSIZE) { 1869 info->dls_size = size; 1870 info->dls_cnt = cnt; 1871 } 1872 } 1873 1874 /* 1875 * Return the origin of the object associated with this link-map. 1876 * Basically return the dirname(1) of the objects fullpath. 1877 */ 1878 if (request == RTLD_DI_ORIGIN) { 1879 char *str = (char *)p; 1880 1881 (void) strncpy(str, ORIGNAME(lmp), DIRSZ(lmp)); 1882 str += DIRSZ(lmp); 1883 *str = '\0'; 1884 1885 return (0); 1886 } 1887 1888 return (0); 1889 } 1890 1891 #pragma weak _dlinfo = dlinfo 1892 1893 /* 1894 * External entry for dlinfo(3dl). 1895 */ 1896 int 1897 dlinfo(void *handle, int request, void *p) 1898 { 1899 int error, entry; 1900 Rt_map *clmp; 1901 1902 entry = enter(0); 1903 1904 clmp = _caller(caller(), CL_EXECDEF); 1905 1906 error = dlinfo_core(handle, request, p, clmp); 1907 1908 if (entry) 1909 leave(LIST(clmp), 0); 1910 return (error); 1911 } 1912