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 (c) 1988 AT&T 24 * All Rights Reserved 25 * 26 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved. 27 */ 28 29 /* 30 * Programmatic interface to the run_time linker. 31 */ 32 33 #include <sys/debug.h> 34 #include <stdio.h> 35 #include <string.h> 36 #include <dlfcn.h> 37 #include <synch.h> 38 #include <limits.h> 39 #include <debug.h> 40 #include <conv.h> 41 #include "_rtld.h" 42 #include "_audit.h" 43 #include "_elf.h" 44 #include "_inline_gen.h" 45 #include "msg.h" 46 47 /* 48 * Determine who called us - given a pc determine in which object it resides. 49 * 50 * For dlopen() the link map of the caller must be passed to load_so() so that 51 * the appropriate search rules (4.x or 5.0) are used to locate any 52 * dependencies. Also, if we've been called from a 4.x module it may be 53 * necessary to fix the specified pathname so that it conforms with the 5.0 elf 54 * rules. 55 * 56 * For dlsym() the link map of the caller is used to determine RTLD_NEXT 57 * requests, together with requests based off of a dlopen(0). 58 * For dladdr() this routines provides a generic means of scanning all loaded 59 * segments. 60 */ 61 Rt_map * 62 _caller(caddr_t cpc, int flags) 63 { 64 Lm_list *lml; 65 Aliste idx1; 66 67 for (APLIST_TRAVERSE(dynlm_list, idx1, lml)) { 68 Aliste idx2; 69 Lm_cntl *lmc; 70 71 for (ALIST_TRAVERSE(lml->lm_lists, idx2, lmc)) { 72 Rt_map *lmp; 73 74 for (lmp = lmc->lc_head; lmp; 75 lmp = NEXT_RT_MAP(lmp)) { 76 77 if (find_segment(cpc, lmp)) 78 return (lmp); 79 } 80 } 81 } 82 83 /* 84 * No mapping can be determined. If asked for a default, assume this 85 * is from the executable. 86 */ 87 if (flags & CL_EXECDEF) 88 return ((Rt_map *)lml_main.lm_head); 89 90 return (0); 91 } 92 93 #pragma weak _dlerror = dlerror 94 95 /* 96 * External entry for dlerror(3dl). Returns a pointer to the string describing 97 * the last occurring error. The last occurring error is cleared. 98 */ 99 char * 100 dlerror() 101 { 102 char *error; 103 Rt_map *clmp; 104 int entry; 105 106 entry = enter(0); 107 108 clmp = _caller(caller(), CL_EXECDEF); 109 110 DBG_CALL(Dbg_dl_dlerror(clmp, lasterr)); 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. On success, returns the group descriptor, and if alep is non-NULL 123 * the *alep is set to ALE_EXISTS if the dependency already exists, or to 124 * ALE_CREATE if the dependency is newly created. 125 */ 126 Grp_desc * 127 hdl_add(Grp_hdl *ghp, Rt_map *lmp, uint_t dflags, int *alep) 128 { 129 Grp_desc *gdp; 130 Aliste idx; 131 int ale = ALE_CREATE; 132 uint_t oflags; 133 134 /* 135 * Make sure this dependency hasn't already been recorded. 136 */ 137 for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) { 138 if (gdp->gd_depend == lmp) { 139 ale = ALE_EXISTS; 140 break; 141 } 142 } 143 144 if (ale == ALE_CREATE) { 145 Grp_desc gd; 146 147 /* 148 * Create a new handle descriptor. 149 */ 150 gd.gd_depend = lmp; 151 gd.gd_flags = 0; 152 153 /* 154 * Indicate this object is a part of this handles group. 155 */ 156 if (aplist_append(&GROUPS(lmp), ghp, AL_CNT_GROUPS) == NULL) 157 return (NULL); 158 159 /* 160 * Append the new dependency to this handle. 161 */ 162 if ((gdp = alist_append(&ghp->gh_depends, &gd, 163 sizeof (Grp_desc), AL_CNT_DEPENDS)) == NULL) 164 return (NULL); 165 } 166 167 oflags = gdp->gd_flags; 168 gdp->gd_flags |= dflags; 169 170 if (DBG_ENABLED) { 171 if (ale == ALE_CREATE) 172 DBG_CALL(Dbg_file_hdl_action(ghp, lmp, DBG_DEP_ADD, 173 gdp->gd_flags)); 174 else if (gdp->gd_flags != oflags) 175 DBG_CALL(Dbg_file_hdl_action(ghp, lmp, DBG_DEP_UPDATE, 176 gdp->gd_flags)); 177 } 178 179 if (alep) 180 *alep = ale; 181 return (gdp); 182 } 183 184 /* 185 * Create a handle. 186 * 187 * rlmp - represents the reference link-map for which the handle is being 188 * created. 189 * clmp - represents the caller who is requesting the handle. 190 * hflags - provide group handle flags (GPH_*) that affect the use of the 191 * handle, such as dlopen(0), or use or use of RTLD_FIRST. 192 * rdflags - provide group dependency flags for the reference link-map rlmp, 193 * such as whether the dependency can be used for dlsym(), can be 194 * relocated against, or whether this objects dependencies should 195 * be processed. 196 * cdflags - provide group dependency flags for the caller. 197 */ 198 Grp_hdl * 199 hdl_create(Lm_list *lml, Rt_map *rlmp, Rt_map *clmp, uint_t hflags, 200 uint_t rdflags, uint_t cdflags) 201 { 202 Grp_hdl *ghp = NULL, *aghp; 203 APlist **alpp; 204 Aliste idx; 205 206 /* 207 * For dlopen(0) the handle is maintained as part of the link-map list, 208 * otherwise the handle is associated with the reference link-map. 209 */ 210 if (hflags & GPH_ZERO) 211 alpp = &(lml->lm_handle); 212 else 213 alpp = &(HANDLES(rlmp)); 214 215 /* 216 * Objects can contain multiple handles depending on the handle flags 217 * supplied. Most RTLD flags pertain to the object itself and the 218 * bindings that it can achieve. Multiple handles for these flags 219 * don't make sense. But if the flag determines how the handle might 220 * be used, then multiple handles may exist. Presently this only makes 221 * sense for RTLD_FIRST. Determine if an appropriate handle already 222 * exists. 223 */ 224 for (APLIST_TRAVERSE(*alpp, idx, aghp)) { 225 if ((aghp->gh_flags & GPH_FIRST) == (hflags & GPH_FIRST)) { 226 ghp = aghp; 227 break; 228 } 229 } 230 231 if (ghp == NULL) { 232 uint_t ndx; 233 234 /* 235 * If this is the first request for this handle, allocate and 236 * initialize a new handle. 237 */ 238 DBG_CALL(Dbg_file_hdl_title(DBG_HDL_CREATE)); 239 240 if ((ghp = malloc(sizeof (Grp_hdl))) == NULL) 241 return (NULL); 242 243 /* 244 * Associate the handle with the link-map list or the reference 245 * link-map as appropriate. 246 */ 247 if (aplist_append(alpp, ghp, AL_CNT_GROUPS) == NULL) { 248 free(ghp); 249 return (NULL); 250 } 251 252 /* 253 * Record the existence of this handle for future verification. 254 */ 255 /* LINTED */ 256 ndx = (uintptr_t)ghp % HDLIST_SZ; 257 258 if (aplist_append(&hdl_alp[ndx], ghp, AL_CNT_HANDLES) == NULL) { 259 (void) aplist_delete_value(*alpp, ghp); 260 free(ghp); 261 return (NULL); 262 } 263 264 ghp->gh_depends = NULL; 265 ghp->gh_refcnt = 1; 266 ghp->gh_flags = hflags; 267 268 /* 269 * A dlopen(0) handle is identified by the GPH_ZERO flag, the 270 * head of the link-map list is defined as the owner. There is 271 * no need to maintain a list of dependencies, for when this 272 * handle is used (for dlsym()) a dynamic search through the 273 * entire link-map list provides for searching all objects with 274 * GLOBAL visibility. 275 */ 276 if (hflags & GPH_ZERO) { 277 ghp->gh_ownlmp = lml->lm_head; 278 ghp->gh_ownlml = lml; 279 } else { 280 ghp->gh_ownlmp = rlmp; 281 ghp->gh_ownlml = LIST(rlmp); 282 283 if (hdl_add(ghp, rlmp, rdflags, NULL) == NULL) 284 return (NULL); 285 286 /* 287 * If this new handle is a private handle, there's no 288 * need to track the caller, so we're done. 289 */ 290 if (hflags & GPH_PRIVATE) 291 return (ghp); 292 293 /* 294 * If this new handle is public, and isn't a special 295 * handle representing ld.so.1, indicate that a local 296 * group now exists. This state allows singleton 297 * searches to be optimized. 298 */ 299 if ((hflags & GPH_LDSO) == 0) 300 LIST(rlmp)->lm_flags |= LML_FLG_GROUPSEXIST; 301 } 302 } else { 303 /* 304 * If a handle already exists, bump its reference count. 305 * 306 * If the previous reference count was 0, then this is a handle 307 * that an earlier call to dlclose() was unable to remove. Such 308 * handles are put on the orphan list. As this handle is back 309 * in use, it must be removed from the orphan list. 310 * 311 * Note, handles associated with a link-map list itself (i.e. 312 * dlopen(0)) can have a reference count of 0. However, these 313 * handles are never deleted, and therefore are never moved to 314 * the orphan list. 315 */ 316 if ((ghp->gh_refcnt++ == 0) && 317 ((ghp->gh_flags & GPH_ZERO) == 0)) { 318 uint_t ndx; 319 320 /* LINTED */ 321 ndx = (uintptr_t)ghp % HDLIST_SZ; 322 323 (void) aplist_delete_value(hdl_alp[HDLIST_ORP], ghp); 324 (void) aplist_append(&hdl_alp[ndx], ghp, 325 AL_CNT_HANDLES); 326 327 if (DBG_ENABLED) { 328 Aliste idx; 329 Grp_desc *gdp; 330 331 DBG_CALL(Dbg_file_hdl_title(DBG_HDL_REINST)); 332 for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) 333 DBG_CALL(Dbg_file_hdl_action(ghp, 334 gdp->gd_depend, DBG_DEP_REINST, 0)); 335 } 336 } 337 338 /* 339 * If we've been asked to create a private handle, there's no 340 * need to track the caller. 341 */ 342 if (hflags & GPH_PRIVATE) { 343 /* 344 * Negate the reference count increment. 345 */ 346 ghp->gh_refcnt--; 347 return (ghp); 348 } else { 349 /* 350 * If a private handle already exists, promote this 351 * handle to public by initializing both the reference 352 * count and the handle flags. 353 */ 354 if (ghp->gh_flags & GPH_PRIVATE) { 355 ghp->gh_refcnt = 1; 356 ghp->gh_flags &= ~GPH_PRIVATE; 357 ghp->gh_flags |= hflags; 358 } 359 } 360 } 361 362 /* 363 * Keep track of the parent (caller). As this object can be referenced 364 * by different parents, this processing is carried out every time a 365 * handle is requested. 366 */ 367 if (clmp && (hdl_add(ghp, clmp, cdflags, NULL) == NULL)) 368 return (NULL); 369 370 return (ghp); 371 } 372 373 /* 374 * Initialize a handle that has been created for an object that is already 375 * loaded. The handle is initialized with the present dependencies of that 376 * object. Once this initialization has occurred, any new objects that might 377 * be loaded as dependencies (lazy-loading) are added to the handle as each new 378 * object is loaded. 379 */ 380 int 381 hdl_initialize(Grp_hdl *ghp, Rt_map *nlmp, int mode, int promote) 382 { 383 Aliste idx; 384 Grp_desc *gdp; 385 386 /* 387 * If the handle has already been initialized, and the initial object's 388 * mode hasn't been promoted, there's no need to recompute the modes of 389 * any dependencies. If the object we've added has just been opened, 390 * the objects dependencies will not yet have been processed. These 391 * dependencies will be added on later calls to load_one(). Otherwise, 392 * this object already exists, so add all of its dependencies to the 393 * handle were operating on. 394 */ 395 if (((ghp->gh_flags & GPH_INITIAL) && (promote == 0)) || 396 ((FLAGS(nlmp) & FLG_RT_ANALYZED) == 0)) { 397 ghp->gh_flags |= GPH_INITIAL; 398 return (1); 399 } 400 401 DBG_CALL(Dbg_file_hdl_title(DBG_HDL_ADD)); 402 for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) { 403 Rt_map *lmp = gdp->gd_depend; 404 Aliste idx1; 405 Bnd_desc *bdp; 406 407 /* 408 * If this dependency doesn't indicate that its dependencies 409 * should be added to a handle, ignore it. This case identifies 410 * a parent of a dlopen(RTLD_PARENT) request. 411 */ 412 if ((gdp->gd_flags & GPD_ADDEPS) == 0) 413 continue; 414 415 for (APLIST_TRAVERSE(DEPENDS(lmp), idx1, bdp)) { 416 Rt_map *dlmp = bdp->b_depend; 417 418 if ((bdp->b_flags & BND_NEEDED) == 0) 419 continue; 420 421 if (hdl_add(ghp, dlmp, 422 (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS), NULL) == NULL) 423 return (0); 424 425 (void) update_mode(dlmp, MODE(dlmp), mode); 426 } 427 } 428 ghp->gh_flags |= GPH_INITIAL; 429 return (1); 430 } 431 432 /* 433 * Sanity check a program-provided handle. 434 */ 435 static int 436 hdl_validate(Grp_hdl *ghp) 437 { 438 Aliste idx; 439 Grp_hdl *lghp; 440 uint_t ndx; 441 442 /* LINTED */ 443 ndx = (uintptr_t)ghp % HDLIST_SZ; 444 445 for (APLIST_TRAVERSE(hdl_alp[ndx], idx, lghp)) { 446 if ((lghp == ghp) && (ghp->gh_refcnt != 0)) 447 return (1); 448 } 449 return (0); 450 } 451 452 /* 453 * Core dlclose activity. 454 */ 455 int 456 dlclose_core(Grp_hdl *ghp, Rt_map *clmp, Lm_list *lml) 457 { 458 int error; 459 460 /* 461 * If we're already at atexit() there's no point processing further, 462 * all objects have already been tsorted for fini processing. 463 */ 464 if (rtld_flags & RT_FL_ATEXIT) 465 return (0); 466 467 /* 468 * Diagnose what we're up to. 469 */ 470 if (ghp->gh_flags & GPH_ZERO) { 471 DBG_CALL(Dbg_dl_dlclose(clmp, MSG_ORIG(MSG_STR_ZERO), 472 DBG_DLCLOSE_IGNORE)); 473 } else { 474 DBG_CALL(Dbg_dl_dlclose(clmp, NAME(ghp->gh_ownlmp), 475 DBG_DLCLOSE_NULL)); 476 } 477 478 /* 479 * Decrement reference count of this object. 480 */ 481 if (--(ghp->gh_refcnt)) 482 return (0); 483 484 /* 485 * If this handle is special (dlopen(0)), then leave it around - it 486 * has little overhead. 487 */ 488 if (ghp->gh_flags & GPH_ZERO) 489 return (0); 490 491 /* 492 * This handle is no longer being referenced, remove it. If this handle 493 * is part of an alternative link-map list, determine if the whole list 494 * can be removed also. 495 */ 496 error = remove_hdl(ghp, clmp, NULL); 497 498 if ((lml->lm_flags & (LML_FLG_BASELM | LML_FLG_RTLDLM)) == 0) 499 remove_lml(lml); 500 501 return (error); 502 } 503 504 /* 505 * Internal dlclose activity. Called from user level or directly for internal 506 * error cleanup. 507 */ 508 int 509 dlclose_intn(Grp_hdl *ghp, Rt_map *clmp) 510 { 511 Rt_map *nlmp = NULL; 512 Lm_list *olml = NULL; 513 int error; 514 515 /* 516 * Although we're deleting object(s) it's quite possible that additional 517 * objects get loaded from running the .fini section(s) of the objects 518 * being deleted. These objects will have been added to the same 519 * link-map list as those objects being deleted. Remember this list 520 * for later investigation. 521 */ 522 olml = ghp->gh_ownlml; 523 524 error = dlclose_core(ghp, clmp, olml); 525 526 /* 527 * Determine whether the original link-map list still exists. In the 528 * case of a dlclose of an alternative (dlmopen) link-map the whole 529 * list may have been removed. 530 */ 531 if (olml) { 532 Aliste idx; 533 Lm_list *lml; 534 535 for (APLIST_TRAVERSE(dynlm_list, idx, lml)) { 536 if (olml == lml) { 537 nlmp = olml->lm_head; 538 break; 539 } 540 } 541 } 542 load_completion(nlmp); 543 return (error); 544 } 545 546 /* 547 * Argument checking for dlclose. Only called via external entry. 548 */ 549 static int 550 dlclose_check(void *handle, Rt_map *clmp) 551 { 552 Grp_hdl *ghp = (Grp_hdl *)handle; 553 554 if (hdl_validate(ghp) == 0) { 555 Conv_inv_buf_t inv_buf; 556 557 (void) conv_invalid_val(&inv_buf, EC_NATPTR(ghp), 0); 558 DBG_CALL(Dbg_dl_dlclose(clmp, inv_buf.buf, DBG_DLCLOSE_NULL)); 559 560 eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL), 561 EC_NATPTR(handle)); 562 return (1); 563 } 564 return (dlclose_intn(ghp, clmp)); 565 } 566 567 #pragma weak _dlclose = dlclose 568 569 /* 570 * External entry for dlclose(3dl). Returns 0 for success, non-zero otherwise. 571 */ 572 int 573 dlclose(void *handle) 574 { 575 int error, entry; 576 Rt_map *clmp; 577 578 entry = enter(0); 579 580 clmp = _caller(caller(), CL_EXECDEF); 581 582 error = dlclose_check(handle, clmp); 583 584 if (entry) 585 leave(LIST(clmp), 0); 586 return (error); 587 } 588 589 static uint_t lmid = 0; 590 591 /* 592 * The addition of new link-map lists is assumed to be in small quantities. 593 * Here, we assign a unique link-map id for diagnostic use. Simply update the 594 * running link-map count until we max out. 595 */ 596 int 597 newlmid(Lm_list *lml) 598 { 599 char buffer[MSG_LMID_ALT_SIZE + 12]; 600 601 if (lmid == UINT_MAX) { 602 lml->lm_lmid = UINT_MAX; 603 (void) strncpy(buffer, MSG_ORIG(MSG_LMID_MAXED), 604 MSG_LMID_ALT_SIZE + 12); 605 } else { 606 lml->lm_lmid = lmid++; 607 (void) snprintf(buffer, MSG_LMID_ALT_SIZE + 12, 608 MSG_ORIG(MSG_LMID_FMT), MSG_ORIG(MSG_LMID_ALT), 609 lml->lm_lmid); 610 } 611 if ((lml->lm_lmidstr = strdup(buffer)) == NULL) 612 return (0); 613 614 return (1); 615 } 616 617 /* 618 * Core dlopen activity. 619 */ 620 static Grp_hdl * 621 dlmopen_core(Lm_list *lml, Lm_list *olml, const char *path, int mode, 622 Rt_map *clmp, uint_t flags, uint_t orig, int *in_nfavl) 623 { 624 Alist *palp = NULL; 625 Rt_map *nlmp; 626 Grp_hdl *ghp; 627 Aliste olmco, nlmco; 628 629 DBG_CALL(Dbg_dl_dlopen(clmp, 630 (path ? path : MSG_ORIG(MSG_STR_ZERO)), in_nfavl, mode)); 631 632 /* 633 * Having diagnosed the originally defined modes, assign any defaults 634 * or corrections. 635 */ 636 if (((mode & (RTLD_GROUP | RTLD_WORLD)) == 0) && 637 ((mode & RTLD_NOLOAD) == 0)) 638 mode |= (RTLD_GROUP | RTLD_WORLD); 639 if ((mode & RTLD_NOW) && (rtld_flags2 & RT_FL2_BINDLAZY)) { 640 mode &= ~RTLD_NOW; 641 mode |= RTLD_LAZY; 642 } 643 644 /* 645 * If the path specified is null then we're operating on global 646 * objects. Associate a dummy handle with the link-map list. 647 */ 648 if (path == NULL) { 649 Grp_hdl *ghp; 650 uint_t hflags, rdflags, cdflags; 651 int promote = 0; 652 653 /* 654 * Establish any flags for the handle (Grp_hdl). 655 * 656 * - This is a dummy, public, handle (0) that provides for a 657 * dynamic search of all global objects within the process. 658 * - Use of the RTLD_FIRST mode indicates that only the first 659 * dependency on the handle (the referenced object) can be 660 * used to satisfy dlsym() requests. 661 */ 662 hflags = (GPH_PUBLIC | GPH_ZERO); 663 if (mode & RTLD_FIRST) 664 hflags |= GPH_FIRST; 665 666 /* 667 * Establish the flags for the referenced dependency descriptor 668 * (Grp_desc). 669 * 670 * - The referenced object is available for dlsym(). 671 * - The referenced object is available to relocate against. 672 * - The referenced object should have it's dependencies 673 * added to this handle. 674 */ 675 rdflags = (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS); 676 677 /* 678 * Establish the flags for this callers dependency descriptor 679 * (Grp_desc). 680 * 681 * - The explicit creation of a handle creates a descriptor 682 * for the referenced object and the parent (caller). 683 * - Use of the RTLD_PARENT flag indicates that the parent 684 * can be relocated against. 685 */ 686 cdflags = GPD_PARENT; 687 if (mode & RTLD_PARENT) 688 cdflags |= GPD_RELOC; 689 690 if ((ghp = hdl_create(lml, 0, clmp, hflags, rdflags, 691 cdflags)) == NULL) 692 return (NULL); 693 694 /* 695 * Traverse the main link-map control list, updating the mode 696 * of any objects as necessary. Call the relocation engine if 697 * this mode promotes the existing state of any relocations. 698 * crle()'s first pass loads all objects necessary for building 699 * a configuration file, however none of them are relocated. 700 * crle()'s second pass relocates objects in preparation for 701 * dldump()'ing using dlopen(0, RTLD_NOW). 702 */ 703 if ((mode & (RTLD_NOW | RTLD_CONFGEN)) == RTLD_CONFGEN) 704 return (ghp); 705 706 for (nlmp = lml->lm_head; nlmp; nlmp = NEXT_RT_MAP(nlmp)) { 707 if (((MODE(nlmp) & RTLD_GLOBAL) == 0) || 708 (FLAGS(nlmp) & FLG_RT_DELETE)) 709 continue; 710 711 if (update_mode(nlmp, MODE(nlmp), mode)) 712 promote = 1; 713 } 714 if (promote) 715 (void) relocate_lmc(lml, ALIST_OFF_DATA, clmp, 716 lml->lm_head, in_nfavl); 717 718 return (ghp); 719 } 720 721 /* 722 * Fix the pathname. If this object expands to multiple paths (ie. 723 * $ISALIST or $HWCAP have been used), then make sure the user has also 724 * furnished the RTLD_FIRST flag. As yet, we don't support opening 725 * more than one object at a time, so enforcing the RTLD_FIRST flag 726 * provides flexibility should we be able to support dlopening more 727 * than one object in the future. 728 */ 729 if (LM_FIX_NAME(clmp)(path, clmp, &palp, AL_CNT_NEEDED, orig) == NULL) 730 return (NULL); 731 732 if ((palp->al_arritems > 1) && ((mode & RTLD_FIRST) == 0)) { 733 remove_plist(&palp, 1); 734 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_5)); 735 return (NULL); 736 } 737 738 /* 739 * Establish a link-map control list for this request, and load the 740 * associated object. 741 */ 742 if ((nlmco = create_cntl(lml, 1)) == NULL) { 743 remove_plist(&palp, 1); 744 return (NULL); 745 } 746 olmco = nlmco; 747 748 nlmp = load_one(lml, nlmco, palp, clmp, mode, (flags | FLG_RT_PUBHDL), 749 &ghp, in_nfavl); 750 751 /* 752 * Remove any expanded pathname infrastructure, and if the dependency 753 * couldn't be loaded, cleanup. 754 */ 755 remove_plist(&palp, 1); 756 if (nlmp == NULL) { 757 remove_cntl(lml, olmco); 758 return (NULL); 759 } 760 761 /* 762 * If loading an auditor was requested, and the auditor already existed, 763 * then the link-map returned will be to the original auditor. The new 764 * link-map list that was initially created, and the associated link-map 765 * control list are no longer needed. As the auditor is already loaded, 766 * we're probably done, but fall through in case additional relocations 767 * would be triggered by the mode of the caller. 768 */ 769 if ((flags & FLG_RT_AUDIT) && (LIST(nlmp) != lml)) { 770 remove_cntl(lml, olmco); 771 lml = LIST(nlmp); 772 olmco = 0; 773 nlmco = ALIST_OFF_DATA; 774 } 775 776 /* 777 * Finish processing the objects associated with this request. 778 */ 779 if (((nlmp = analyze_lmc(lml, nlmco, nlmp, in_nfavl)) == NULL) || 780 (relocate_lmc(lml, nlmco, clmp, nlmp, in_nfavl) == 0)) { 781 ghp = NULL; 782 nlmp = NULL; 783 } 784 785 /* 786 * If the dlopen has failed, clean up any objects that might have been 787 * loaded successfully on this new link-map control list. 788 */ 789 if (olmco && (nlmp == NULL)) 790 remove_lmc(lml, clmp, olmco, path); 791 792 /* 793 * Finally, remove any temporary link-map control list. Note, if this 794 * operation successfully established a new link-map list, then a base 795 * link-map control list will have been created, which must remain. 796 */ 797 if (olmco && ((nlmp == NULL) || (olml != (Lm_list *)LM_ID_NEWLM))) 798 remove_cntl(lml, olmco); 799 800 return (ghp); 801 } 802 803 /* 804 * dlopen() and dlsym() operations are the means by which a process can 805 * test for the existence of required dependencies. If the necessary 806 * dependencies don't exist, then associated functionality can't be used. 807 * However, the lack of dependencies can be fixed, and the dlopen() and 808 * dlsym() requests can be repeated. As we use a "not-found" AVL tree to 809 * cache any failed full path loads, secondary dlopen() and dlsym() requests 810 * will fail, even if the dependencies have been installed. 811 * 812 * dlopen() and dlsym() retry any failures by removing the "not-found" AVL 813 * tree. Should any dependencies be found, their names are added to the 814 * FullPath AVL tree. This routine removes any new "not-found" AVL tree, 815 * so that the dlopen() or dlsym() can replace the original "not-found" tree. 816 */ 817 inline static void 818 nfavl_remove(avl_tree_t *avlt) 819 { 820 PathNode *pnp; 821 void *cookie = NULL; 822 823 if (avlt) { 824 while ((pnp = avl_destroy_nodes(avlt, &cookie)) != NULL) 825 free(pnp); 826 827 avl_destroy(avlt); 828 free(avlt); 829 } 830 } 831 832 /* 833 * Internal dlopen() activity. Called from user level or directly for internal 834 * opens that require a handle. 835 */ 836 Grp_hdl * 837 dlmopen_intn(Lm_list *lml, const char *path, int mode, Rt_map *clmp, 838 uint_t flags, uint_t orig) 839 { 840 Lm_list *olml = lml; 841 Rt_map *dlmp = NULL; 842 Grp_hdl *ghp; 843 int in_nfavl = 0; 844 845 /* 846 * Check for magic link-map list values: 847 * 848 * LM_ID_BASE: Operate on the PRIMARY (executables) link map 849 * LM_ID_LDSO: Operation on ld.so.1's link map 850 * LM_ID_NEWLM: Create a new link-map. 851 */ 852 if (lml == (Lm_list *)LM_ID_NEWLM) { 853 if ((lml = calloc(sizeof (Lm_list), 1)) == NULL) 854 return (NULL); 855 856 /* 857 * Establish the new link-map flags from the callers and those 858 * explicitly provided. 859 */ 860 lml->lm_tflags = LIST(clmp)->lm_tflags; 861 if (flags & FLG_RT_AUDIT) { 862 /* 863 * Unset any auditing flags - an auditor shouldn't be 864 * audited. Insure all audit dependencies are loaded. 865 */ 866 lml->lm_tflags &= ~LML_TFLG_AUD_MASK; 867 lml->lm_tflags |= 868 (LML_TFLG_NOLAZYLD | LML_TFLG_LOADFLTR); 869 lml->lm_flags |= LML_FLG_NOAUDIT; 870 } 871 872 if (aplist_append(&dynlm_list, lml, AL_CNT_DYNLIST) == NULL) { 873 free(lml); 874 return (NULL); 875 } 876 if (newlmid(lml) == 0) { 877 (void) aplist_delete_value(dynlm_list, lml); 878 free(lml); 879 return (NULL); 880 } 881 } else if ((uintptr_t)lml < LM_ID_NUM) { 882 if ((uintptr_t)lml == LM_ID_BASE) 883 lml = &lml_main; 884 else if ((uintptr_t)lml == LM_ID_LDSO) 885 lml = &lml_rtld; 886 } 887 888 /* 889 * Open the required object on the associated link-map list. 890 */ 891 ghp = dlmopen_core(lml, olml, path, mode, clmp, flags, orig, &in_nfavl); 892 893 /* 894 * If the object could not be found it is possible that the "not-found" 895 * AVL tree had indicated that the file does not exist. In case the 896 * file system has changed since this "not-found" recording was made, 897 * retry the dlopen() with a clean "not-found" AVL tree. 898 */ 899 if ((ghp == NULL) && in_nfavl) { 900 avl_tree_t *oavlt = nfavl; 901 902 nfavl = NULL; 903 ghp = dlmopen_core(lml, olml, path, mode, clmp, flags, orig, 904 NULL); 905 906 /* 907 * If the file is found, then its full path name will have been 908 * registered in the FullPath AVL tree. Remove any new 909 * "not-found" AVL information, and restore the former AVL tree. 910 */ 911 nfavl_remove(nfavl); 912 nfavl = oavlt; 913 } 914 915 /* 916 * Establish the new link-map from which .init processing will begin. 917 * Ignore .init firing when constructing a configuration file (crle(1)). 918 */ 919 if (ghp && ((mode & RTLD_CONFGEN) == 0)) 920 dlmp = ghp->gh_ownlmp; 921 922 /* 923 * If loading an auditor was requested, and the auditor already existed, 924 * then the link-map returned will be to the original auditor. Remove 925 * the link-map control list that was created for this request. 926 */ 927 if (dlmp && (flags & FLG_RT_AUDIT) && (LIST(dlmp) != lml)) { 928 remove_lml(lml); 929 lml = LIST(dlmp); 930 } 931 932 /* 933 * If this load failed, remove any alternative link-map list. 934 */ 935 if ((ghp == NULL) && 936 ((lml->lm_flags & (LML_FLG_BASELM | LML_FLG_RTLDLM)) == 0)) { 937 remove_lml(lml); 938 lml = NULL; 939 } 940 941 /* 942 * Finish this load request. If objects were loaded, .init processing 943 * is computed. Finally, the debuggers are informed of the link-map 944 * lists being stable. 945 */ 946 load_completion(dlmp); 947 948 return (ghp); 949 } 950 951 /* 952 * Argument checking for dlopen. Only called via external entry. 953 */ 954 static Grp_hdl * 955 dlmopen_check(Lm_list *lml, const char *path, int mode, Rt_map *clmp) 956 { 957 /* 958 * Verify that a valid pathname has been supplied. 959 */ 960 if (path && (*path == '\0')) { 961 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLPATH)); 962 return (0); 963 } 964 965 /* 966 * Historically we've always verified the mode is either RTLD_NOW or 967 * RTLD_LAZY. RTLD_NOLOAD is valid by itself. Use of LM_ID_NEWLM 968 * requires a specific pathname, and use of RTLD_PARENT is meaningless. 969 */ 970 if ((mode & (RTLD_NOW | RTLD_LAZY | RTLD_NOLOAD)) == 0) { 971 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_1)); 972 return (0); 973 } 974 if ((mode & (RTLD_NOW | RTLD_LAZY)) == (RTLD_NOW | RTLD_LAZY)) { 975 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_2)); 976 return (0); 977 } 978 if ((lml == (Lm_list *)LM_ID_NEWLM) && (path == NULL)) { 979 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_3)); 980 return (0); 981 } 982 if ((lml == (Lm_list *)LM_ID_NEWLM) && (mode & RTLD_PARENT)) { 983 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_4)); 984 return (0); 985 } 986 987 return (dlmopen_intn(lml, path, mode, clmp, 0, 0)); 988 } 989 990 #pragma weak _dlopen = dlopen 991 992 /* 993 * External entry for dlopen(3dl). On success, returns a pointer (handle) to 994 * the structure containing information about the newly added object, ie. can 995 * be used by dlsym(). On failure, returns a null pointer. 996 */ 997 void * 998 dlopen(const char *path, int mode) 999 { 1000 int entry; 1001 Rt_map *clmp; 1002 Grp_hdl *ghp; 1003 Lm_list *lml; 1004 1005 entry = enter(0); 1006 1007 clmp = _caller(caller(), CL_EXECDEF); 1008 lml = LIST(clmp); 1009 1010 ghp = dlmopen_check(lml, path, mode, clmp); 1011 1012 if (entry) 1013 leave(lml, 0); 1014 return ((void *)ghp); 1015 } 1016 1017 #pragma weak _dlmopen = dlmopen 1018 1019 /* 1020 * External entry for dlmopen(3dl). 1021 */ 1022 void * 1023 dlmopen(Lmid_t lmid, const char *path, int mode) 1024 { 1025 int entry; 1026 Rt_map *clmp; 1027 Grp_hdl *ghp; 1028 1029 entry = enter(0); 1030 1031 clmp = _caller(caller(), CL_EXECDEF); 1032 1033 ghp = dlmopen_check((Lm_list *)lmid, path, mode, clmp); 1034 1035 if (entry) 1036 leave(LIST(clmp), 0); 1037 return ((void *)ghp); 1038 } 1039 1040 /* 1041 * Handle processing for dlsym. 1042 */ 1043 int 1044 dlsym_handle(Grp_hdl *ghp, Slookup *slp, Sresult *srp, uint_t *binfo, 1045 int *in_nfavl) 1046 { 1047 Rt_map *nlmp, * lmp = ghp->gh_ownlmp; 1048 Rt_map *clmp = slp->sl_cmap; 1049 const char *name = slp->sl_name; 1050 Slookup sl = *slp; 1051 1052 sl.sl_flags = (LKUP_FIRST | LKUP_DLSYM | LKUP_SPEC); 1053 1054 /* 1055 * Continue processing a dlsym request. Lookup the required symbol in 1056 * each link-map specified by the handle. 1057 * 1058 * To leverage off of lazy loading, dlsym() requests can result in two 1059 * passes. The first descends the link-maps of any objects already in 1060 * the address space. If the symbol isn't located, and lazy 1061 * dependencies still exist, then a second pass is made to load these 1062 * dependencies if applicable. This model means that in the case where 1063 * a symbol exists in more than one object, the one located may not be 1064 * constant - this is the standard issue with lazy loading. In addition, 1065 * attempting to locate a symbol that doesn't exist will result in the 1066 * loading of all lazy dependencies on the given handle, which can 1067 * defeat some of the advantages of lazy loading (look out JVM). 1068 */ 1069 if (ghp->gh_flags & GPH_ZERO) { 1070 Lm_list *lml; 1071 uint_t lazy = 0; 1072 1073 /* 1074 * If this symbol lookup is triggered from a dlopen(0) handle, 1075 * traverse the present link-map list looking for promiscuous 1076 * entries. 1077 */ 1078 for (nlmp = lmp; nlmp; nlmp = NEXT_RT_MAP(nlmp)) { 1079 /* 1080 * If this handle indicates we're only to look in the 1081 * first object check whether we're done. 1082 */ 1083 if ((nlmp != lmp) && (ghp->gh_flags & GPH_FIRST)) 1084 return (0); 1085 1086 if (!(MODE(nlmp) & RTLD_GLOBAL)) 1087 continue; 1088 if ((FLAGS(nlmp) & FLG_RT_DELETE) && 1089 ((FLAGS(clmp) & FLG_RT_DELETE) == 0)) 1090 continue; 1091 1092 sl.sl_imap = nlmp; 1093 if (LM_LOOKUP_SYM(clmp)(&sl, srp, binfo, in_nfavl)) 1094 return (1); 1095 1096 /* 1097 * Keep track of any global pending lazy loads. 1098 */ 1099 lazy += LAZY(nlmp); 1100 } 1101 1102 /* 1103 * If we're unable to locate the symbol and this link-map list 1104 * still has pending lazy dependencies, start loading them in an 1105 * attempt to exhaust the search. Note that as we're already 1106 * traversing a dynamic linked list of link-maps there's no 1107 * need for elf_lazy_find_sym() to descend the link-maps itself. 1108 */ 1109 lml = LIST(lmp); 1110 if (lazy) { 1111 DBG_CALL(Dbg_syms_lazy_rescan(lml, name)); 1112 1113 sl.sl_flags |= LKUP_NODESCENT; 1114 1115 for (nlmp = lmp; nlmp; nlmp = NEXT_RT_MAP(nlmp)) { 1116 1117 if (!(MODE(nlmp) & RTLD_GLOBAL) || !LAZY(nlmp)) 1118 continue; 1119 if ((FLAGS(nlmp) & FLG_RT_DELETE) && 1120 ((FLAGS(clmp) & FLG_RT_DELETE) == 0)) 1121 continue; 1122 1123 sl.sl_imap = nlmp; 1124 if (elf_lazy_find_sym(&sl, srp, binfo, 1125 in_nfavl)) 1126 return (1); 1127 } 1128 } 1129 } else { 1130 /* 1131 * Traverse the dlopen() handle searching all presently loaded 1132 * link-maps. 1133 */ 1134 Grp_desc *gdp; 1135 Aliste idx; 1136 uint_t lazy = 0; 1137 1138 for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) { 1139 nlmp = gdp->gd_depend; 1140 1141 if ((gdp->gd_flags & GPD_DLSYM) == 0) 1142 continue; 1143 1144 sl.sl_imap = nlmp; 1145 if (LM_LOOKUP_SYM(clmp)(&sl, srp, binfo, in_nfavl)) 1146 return (1); 1147 1148 if (ghp->gh_flags & GPH_FIRST) 1149 return (0); 1150 1151 /* 1152 * Keep track of any pending lazy loads associated 1153 * with this handle. 1154 */ 1155 lazy += LAZY(nlmp); 1156 } 1157 1158 /* 1159 * If we're unable to locate the symbol and this handle still 1160 * has pending lazy dependencies, start loading the lazy 1161 * dependencies, in an attempt to exhaust the search. 1162 */ 1163 if (lazy) { 1164 DBG_CALL(Dbg_syms_lazy_rescan(LIST(lmp), name)); 1165 1166 for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) { 1167 nlmp = gdp->gd_depend; 1168 1169 if (((gdp->gd_flags & GPD_DLSYM) == 0) || 1170 (LAZY(nlmp) == 0)) 1171 continue; 1172 1173 sl.sl_imap = nlmp; 1174 if (elf_lazy_find_sym(&sl, srp, binfo, 1175 in_nfavl)) 1176 return (1); 1177 } 1178 } 1179 } 1180 return (0); 1181 } 1182 1183 /* 1184 * Determine whether a symbol resides in a caller. This may be a reference, 1185 * which is associated with a specific dependency. 1186 */ 1187 inline static Sym * 1188 sym_lookup_in_caller(Rt_map *clmp, Slookup *slp, Sresult *srp, uint_t *binfo) 1189 { 1190 if (THIS_IS_ELF(clmp) && SYMINTP(clmp)(slp, srp, binfo, NULL)) { 1191 Sym *sym = srp->sr_sym; 1192 1193 slp->sl_rsymndx = (((ulong_t)sym - 1194 (ulong_t)SYMTAB(clmp)) / SYMENT(clmp)); 1195 slp->sl_rsym = sym; 1196 return (sym); 1197 } 1198 return (NULL); 1199 } 1200 1201 /* 1202 * Core dlsym activity. Selects symbol lookup method from handle. 1203 */ 1204 static void * 1205 dlsym_core(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp, 1206 int *in_nfavl) 1207 { 1208 Sym *sym; 1209 int ret = 0; 1210 Syminfo *sip; 1211 Slookup sl; 1212 Sresult sr; 1213 uint_t binfo; 1214 1215 /* 1216 * Initialize the symbol lookup data structure. 1217 * 1218 * Standard relocations are evaluated using the symbol index of the 1219 * associated relocation symbol. This index provides for loading 1220 * any lazy dependency and establishing a direct binding if necessary. 1221 * If a dlsym() operation originates from an object that contains a 1222 * symbol table entry for the same name, then we need to establish the 1223 * symbol index so that any dependency requirements can be triggered. 1224 * 1225 * Therefore, the first symbol lookup that is carried out is for the 1226 * symbol name within the calling object. If this symbol exists, the 1227 * symbols index is computed, added to the Slookup data, and thus used 1228 * to seed the real symbol lookup. 1229 */ 1230 SLOOKUP_INIT(sl, name, clmp, clmp, ld_entry_cnt, elf_hash(name), 1231 0, 0, 0, LKUP_SYMNDX); 1232 SRESULT_INIT(sr, name); 1233 sym = sym_lookup_in_caller(clmp, &sl, &sr, &binfo); 1234 1235 SRESULT_INIT(sr, name); 1236 1237 if (sym && (ELF_ST_VISIBILITY(sym->st_other) == STV_SINGLETON)) { 1238 Rt_map *hlmp = LIST(clmp)->lm_head; 1239 1240 /* 1241 * If a symbol reference is known, and that reference indicates 1242 * that the symbol is a singleton, then the search for the 1243 * symbol must follow the default search path. 1244 */ 1245 DBG_CALL(Dbg_dl_dlsym(clmp, name, in_nfavl, 0, 1246 DBG_DLSYM_SINGLETON)); 1247 1248 sl.sl_imap = hlmp; 1249 if (handle == RTLD_PROBE) 1250 sl.sl_flags = LKUP_NOFALLBACK; 1251 else 1252 sl.sl_flags = LKUP_SPEC; 1253 ret = LM_LOOKUP_SYM(clmp)(&sl, &sr, &binfo, in_nfavl); 1254 1255 } else if (handle == RTLD_NEXT) { 1256 Rt_map *nlmp; 1257 1258 /* 1259 * If this handle is RTLD_NEXT determine whether a lazy load 1260 * from the caller might provide the next object. This mimics 1261 * the lazy loading initialization normally carried out by 1262 * lookup_sym(), however here, we must do this up-front, as 1263 * lookup_sym() will be used to inspect the next object. 1264 */ 1265 if ((sl.sl_rsymndx) && ((sip = SYMINFO(clmp)) != NULL)) { 1266 /* LINTED */ 1267 sip = (Syminfo *)((char *)sip + 1268 (sl.sl_rsymndx * SYMINENT(clmp))); 1269 1270 if ((sip->si_flags & SYMINFO_FLG_DIRECT) && 1271 (sip->si_boundto < SYMINFO_BT_LOWRESERVE)) 1272 (void) elf_lazy_load(clmp, &sl, 1273 sip->si_boundto, name, 0, NULL, in_nfavl); 1274 1275 /* 1276 * Clear the symbol index, so as not to confuse 1277 * lookup_sym() of the next object. 1278 */ 1279 sl.sl_rsymndx = 0; 1280 sl.sl_rsym = NULL; 1281 } 1282 1283 /* 1284 * If the handle is RTLD_NEXT, start searching in the next link 1285 * map from the callers. Determine permissions from the 1286 * present link map. Indicate to lookup_sym() that we're on an 1287 * RTLD_NEXT request so that it will use the callers link map to 1288 * start any possible lazy dependency loading. 1289 */ 1290 sl.sl_imap = nlmp = NEXT_RT_MAP(clmp); 1291 1292 DBG_CALL(Dbg_dl_dlsym(clmp, name, in_nfavl, 1293 (nlmp ? NAME(nlmp) : MSG_INTL(MSG_STR_NULL)), 1294 DBG_DLSYM_NEXT)); 1295 1296 if (nlmp == NULL) 1297 return (0); 1298 1299 sl.sl_flags = LKUP_NEXT; 1300 ret = LM_LOOKUP_SYM(clmp)(&sl, &sr, &binfo, in_nfavl); 1301 1302 } else if (handle == RTLD_SELF) { 1303 /* 1304 * If the handle is RTLD_SELF start searching from the caller. 1305 */ 1306 DBG_CALL(Dbg_dl_dlsym(clmp, name, in_nfavl, NAME(clmp), 1307 DBG_DLSYM_SELF)); 1308 1309 sl.sl_imap = clmp; 1310 sl.sl_flags = (LKUP_SPEC | LKUP_SELF); 1311 ret = LM_LOOKUP_SYM(clmp)(&sl, &sr, &binfo, in_nfavl); 1312 1313 } else if (handle == RTLD_DEFAULT) { 1314 Rt_map *hlmp = LIST(clmp)->lm_head; 1315 1316 /* 1317 * If the handle is RTLD_DEFAULT mimic the standard symbol 1318 * lookup as would be triggered by a relocation. 1319 */ 1320 DBG_CALL(Dbg_dl_dlsym(clmp, name, in_nfavl, 0, 1321 DBG_DLSYM_DEFAULT)); 1322 1323 sl.sl_imap = hlmp; 1324 sl.sl_flags = LKUP_SPEC; 1325 ret = LM_LOOKUP_SYM(clmp)(&sl, &sr, &binfo, in_nfavl); 1326 1327 } else if (handle == RTLD_PROBE) { 1328 Rt_map *hlmp = LIST(clmp)->lm_head; 1329 1330 /* 1331 * If the handle is RTLD_PROBE, mimic the standard symbol 1332 * lookup as would be triggered by a relocation, however do 1333 * not fall back to a lazy loading rescan if the symbol can't be 1334 * found within the currently loaded objects. Note, a lazy 1335 * loaded dependency required by the caller might still get 1336 * loaded to satisfy this request, but no exhaustive lazy load 1337 * rescan is carried out. 1338 */ 1339 DBG_CALL(Dbg_dl_dlsym(clmp, name, in_nfavl, 0, 1340 DBG_DLSYM_PROBE)); 1341 1342 sl.sl_imap = hlmp; 1343 sl.sl_flags = LKUP_NOFALLBACK; 1344 ret = LM_LOOKUP_SYM(clmp)(&sl, &sr, &binfo, in_nfavl); 1345 1346 } else { 1347 Grp_hdl *ghp = (Grp_hdl *)handle; 1348 1349 /* 1350 * Look in the shared object specified by the handle and in all 1351 * of its dependencies. 1352 */ 1353 DBG_CALL(Dbg_dl_dlsym(clmp, name, in_nfavl, 1354 NAME(ghp->gh_ownlmp), DBG_DLSYM_DEF)); 1355 1356 ret = LM_DLSYM(clmp)(ghp, &sl, &sr, &binfo, in_nfavl); 1357 } 1358 1359 if (ret && ((sym = sr.sr_sym) != NULL)) { 1360 Lm_list *lml = LIST(clmp); 1361 Addr addr = sym->st_value; 1362 1363 *dlmp = sr.sr_dmap; 1364 if (!(FLAGS(*dlmp) & FLG_RT_FIXED)) 1365 addr += ADDR(*dlmp); 1366 1367 /* 1368 * Indicate that the defining object is now used. 1369 */ 1370 if (*dlmp != clmp) 1371 FLAGS1(*dlmp) |= FL1_RT_USED; 1372 1373 DBG_CALL(Dbg_bind_global(clmp, 0, 0, (Xword)-1, PLT_T_NONE, 1374 *dlmp, addr, sym->st_value, sr.sr_name, binfo)); 1375 1376 if ((lml->lm_tflags | AFLAGS(clmp)) & LML_TFLG_AUD_SYMBIND) { 1377 uint_t sb_flags = LA_SYMB_DLSYM; 1378 /* LINTED */ 1379 uint_t symndx = (uint_t)(((Xword)sym - 1380 (Xword)SYMTAB(*dlmp)) / SYMENT(*dlmp)); 1381 addr = audit_symbind(clmp, *dlmp, sym, symndx, addr, 1382 &sb_flags); 1383 } 1384 return ((void *)addr); 1385 } 1386 1387 return (NULL); 1388 } 1389 1390 /* 1391 * Internal dlsym activity. Called from user level or directly for internal 1392 * symbol lookup. 1393 */ 1394 void * 1395 dlsym_intn(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp) 1396 { 1397 Rt_map *llmp = NULL; 1398 void *error; 1399 Aliste idx; 1400 Grp_desc *gdp; 1401 int in_nfavl = 0; 1402 1403 /* 1404 * While looking for symbols it's quite possible that additional objects 1405 * get loaded from lazy loading. These objects will have been added to 1406 * the same link-map list as those objects on the handle. Remember this 1407 * list for later investigation. 1408 */ 1409 if ((handle == RTLD_NEXT) || (handle == RTLD_DEFAULT) || 1410 (handle == RTLD_SELF) || (handle == RTLD_PROBE)) 1411 llmp = LIST(clmp)->lm_tail; 1412 else { 1413 Grp_hdl *ghp = (Grp_hdl *)handle; 1414 1415 if (ghp->gh_ownlmp) 1416 llmp = LIST(ghp->gh_ownlmp)->lm_tail; 1417 else { 1418 for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) { 1419 if ((llmp = 1420 LIST(gdp->gd_depend)->lm_tail) != NULL) 1421 break; 1422 } 1423 } 1424 } 1425 1426 error = dlsym_core(handle, name, clmp, dlmp, &in_nfavl); 1427 1428 /* 1429 * If the symbol could not be found it is possible that the "not-found" 1430 * AVL tree had indicated that a required file does not exist. In case 1431 * the file system has changed since this "not-found" recording was 1432 * made, retry the dlsym() with a clean "not-found" AVL tree. 1433 */ 1434 if ((error == NULL) && in_nfavl) { 1435 avl_tree_t *oavlt = nfavl; 1436 1437 nfavl = NULL; 1438 error = dlsym_core(handle, name, clmp, dlmp, NULL); 1439 1440 /* 1441 * If the symbol is found, then any file that was loaded will 1442 * have had its full path name registered in the FullPath AVL 1443 * tree. Remove any new "not-found" AVL information, and 1444 * restore the former AVL tree. 1445 */ 1446 nfavl_remove(nfavl); 1447 nfavl = oavlt; 1448 } 1449 1450 if (error == NULL) { 1451 /* 1452 * Cache the error message, as Java tends to fall through this 1453 * code many times. 1454 */ 1455 if (nosym_str == NULL) 1456 nosym_str = MSG_INTL(MSG_GEN_NOSYM); 1457 eprintf(LIST(clmp), ERR_FATAL, nosym_str, name); 1458 } 1459 1460 load_completion(llmp); 1461 return (error); 1462 } 1463 1464 /* 1465 * Argument checking for dlsym. Only called via external entry. 1466 */ 1467 static void * 1468 dlsym_check(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp) 1469 { 1470 /* 1471 * Verify the arguments. 1472 */ 1473 if (name == NULL) { 1474 eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_ILLSYM)); 1475 return (NULL); 1476 } 1477 if ((handle != RTLD_NEXT) && (handle != RTLD_DEFAULT) && 1478 (handle != RTLD_SELF) && (handle != RTLD_PROBE) && 1479 (hdl_validate((Grp_hdl *)handle) == 0)) { 1480 eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL), 1481 EC_NATPTR(handle)); 1482 return (NULL); 1483 } 1484 return (dlsym_intn(handle, name, clmp, dlmp)); 1485 } 1486 1487 1488 #pragma weak _dlsym = dlsym 1489 1490 /* 1491 * External entry for dlsym(). On success, returns the address of the specified 1492 * symbol. On error returns a null. 1493 */ 1494 void * 1495 dlsym(void *handle, const char *name) 1496 { 1497 int entry; 1498 Rt_map *clmp, *dlmp = NULL; 1499 void *addr; 1500 1501 entry = enter(0); 1502 1503 clmp = _caller(caller(), CL_EXECDEF); 1504 1505 addr = dlsym_check(handle, name, clmp, &dlmp); 1506 1507 if (entry) { 1508 if (dlmp) 1509 is_dep_init(dlmp, clmp); 1510 leave(LIST(clmp), 0); 1511 } 1512 return (addr); 1513 } 1514 1515 /* 1516 * Core dladdr activity. 1517 */ 1518 static void 1519 dladdr_core(Rt_map *almp, void *addr, Dl_info_t *dlip, void **info, int flags) 1520 { 1521 /* 1522 * Set up generic information and any defaults. 1523 */ 1524 dlip->dli_fname = PATHNAME(almp); 1525 1526 dlip->dli_fbase = (void *)ADDR(almp); 1527 dlip->dli_sname = NULL; 1528 dlip->dli_saddr = NULL; 1529 1530 /* 1531 * Determine the nearest symbol to this address. 1532 */ 1533 LM_DLADDR(almp)((ulong_t)addr, almp, dlip, info, flags); 1534 } 1535 1536 #pragma weak _dladdr = dladdr 1537 1538 /* 1539 * External entry for dladdr(3dl) and dladdr1(3dl). Returns an information 1540 * structure that reflects the symbol closest to the address specified. 1541 */ 1542 int 1543 dladdr(void *addr, Dl_info_t *dlip) 1544 { 1545 int entry, ret; 1546 Rt_map *clmp, *almp; 1547 Lm_list *clml; 1548 1549 entry = enter(0); 1550 1551 clmp = _caller(caller(), CL_EXECDEF); 1552 clml = LIST(clmp); 1553 1554 DBG_CALL(Dbg_dl_dladdr(clmp, addr)); 1555 1556 /* 1557 * Use our calling technique to determine what object is associated 1558 * with the supplied address. If a caller can't be determined, 1559 * indicate the failure. 1560 */ 1561 if ((almp = _caller(addr, CL_NONE)) == NULL) { 1562 eprintf(clml, ERR_FATAL, MSG_INTL(MSG_ARG_INVADDR), 1563 EC_NATPTR(addr)); 1564 ret = 0; 1565 } else { 1566 dladdr_core(almp, addr, dlip, 0, 0); 1567 ret = 1; 1568 } 1569 1570 if (entry) 1571 leave(clml, 0); 1572 return (ret); 1573 } 1574 1575 #pragma weak _dladdr1 = dladdr1 1576 1577 int 1578 dladdr1(void *addr, Dl_info_t *dlip, void **info, int flags) 1579 { 1580 int entry, ret = 1; 1581 Rt_map *clmp, *almp; 1582 Lm_list *clml; 1583 1584 entry = enter(0); 1585 1586 clmp = _caller(caller(), CL_EXECDEF); 1587 clml = LIST(clmp); 1588 1589 DBG_CALL(Dbg_dl_dladdr(clmp, addr)); 1590 1591 /* 1592 * Validate any flags. 1593 */ 1594 if (flags) { 1595 int request; 1596 1597 if (((request = (flags & RTLD_DL_MASK)) != RTLD_DL_SYMENT) && 1598 (request != RTLD_DL_LINKMAP)) { 1599 eprintf(clml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLFLAGS), 1600 flags); 1601 ret = 0; 1602 1603 } else if (info == NULL) { 1604 eprintf(clml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLINFO), 1605 flags); 1606 ret = 0; 1607 } 1608 } 1609 1610 /* 1611 * Use our calling technique to determine what object is associated 1612 * with the supplied address. If a caller can't be determined, 1613 * indicate the failure. 1614 */ 1615 if (ret) { 1616 if ((almp = _caller(addr, CL_NONE)) == NULL) { 1617 eprintf(clml, ERR_FATAL, MSG_INTL(MSG_ARG_INVADDR), 1618 EC_NATPTR(addr)); 1619 ret = 0; 1620 } else 1621 dladdr_core(almp, addr, dlip, info, flags); 1622 } 1623 1624 if (entry) 1625 leave(clml, 0); 1626 return (ret); 1627 } 1628 1629 /* 1630 * Core dldump activity. 1631 */ 1632 static int 1633 dldump_core(Rt_map *clmp, Rt_map *lmp, const char *ipath, const char *opath, 1634 int flags) 1635 { 1636 Lm_list *lml = LIST(clmp); 1637 Addr addr = 0; 1638 1639 /* 1640 * Verify any arguments first. 1641 */ 1642 if ((opath == NULL) || (opath[0] == '\0') || 1643 ((lmp == NULL) && (ipath[0] == '\0'))) { 1644 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLPATH)); 1645 return (1); 1646 } 1647 1648 /* 1649 * If an input file is specified make sure its one of our dependencies 1650 * on the main link-map list. Note, this has really all evolved for 1651 * crle(), which uses libcrle.so on an alternative link-map to trigger 1652 * dumping objects from the main link-map list. If we ever want to 1653 * dump objects from alternative link-maps, this model is going to 1654 * have to be revisited. 1655 */ 1656 if (lmp == NULL) { 1657 if ((lmp = is_so_loaded(&lml_main, ipath, NULL)) == NULL) { 1658 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_NOFILE), 1659 ipath); 1660 return (1); 1661 } 1662 if (FLAGS(lmp) & FLG_RT_ALTER) { 1663 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_ALTER), ipath); 1664 return (1); 1665 } 1666 if (FLAGS(lmp) & FLG_RT_NODUMP) { 1667 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_NODUMP), 1668 ipath); 1669 return (1); 1670 } 1671 } 1672 1673 /* 1674 * If the object being dump'ed isn't fixed identify its mapping. 1675 */ 1676 if (!(FLAGS(lmp) & FLG_RT_FIXED)) 1677 addr = ADDR(lmp); 1678 1679 /* 1680 * As rt_dldump() will effectively lazy load the necessary support 1681 * libraries, make sure ld.so.1 is initialized for plt relocations. 1682 */ 1683 if (elf_rtld_load() == 0) 1684 return (0); 1685 1686 /* 1687 * Dump the required image. 1688 */ 1689 return (rt_dldump(lmp, opath, flags, addr)); 1690 } 1691 1692 #pragma weak _dldump = dldump 1693 1694 /* 1695 * External entry for dldump(3c). Returns 0 on success, non-zero otherwise. 1696 */ 1697 int 1698 dldump(const char *ipath, const char *opath, int flags) 1699 { 1700 int error, entry; 1701 Rt_map *clmp, *lmp; 1702 1703 entry = enter(0); 1704 1705 clmp = _caller(caller(), CL_EXECDEF); 1706 1707 if (ipath) { 1708 lmp = NULL; 1709 } else { 1710 lmp = lml_main.lm_head; 1711 ipath = NAME(lmp); 1712 } 1713 1714 DBG_CALL(Dbg_dl_dldump(clmp, ipath, opath, flags)); 1715 1716 error = dldump_core(clmp, lmp, ipath, opath, flags); 1717 1718 if (entry) 1719 leave(LIST(clmp), 0); 1720 return (error); 1721 } 1722 1723 /* 1724 * get_linkmap_id() translates Lm_list * pointers to the Link_map id as used by 1725 * the rtld_db and dlmopen() interfaces. It checks to see if the Link_map is 1726 * one of the primary ones and if so returns it's special token: 1727 * LM_ID_BASE 1728 * LM_ID_LDSO 1729 * 1730 * If it's not one of the primary link_map id's it will instead returns a 1731 * pointer to the Lm_list structure which uniquely identifies the Link_map. 1732 */ 1733 Lmid_t 1734 get_linkmap_id(Lm_list *lml) 1735 { 1736 if (lml->lm_flags & LML_FLG_BASELM) 1737 return (LM_ID_BASE); 1738 if (lml->lm_flags & LML_FLG_RTLDLM) 1739 return (LM_ID_LDSO); 1740 1741 return ((Lmid_t)lml); 1742 } 1743 1744 /* 1745 * Set a new deferred dependency name. 1746 */ 1747 static int 1748 set_def_need(Lm_list *lml, Dyninfo *dyip, const char *name) 1749 { 1750 /* 1751 * If this dependency has already been established, then this dlinfo() 1752 * call is too late. 1753 */ 1754 if (dyip->di_info) { 1755 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_DEF_DEPLOADED), 1756 dyip->di_name); 1757 return (-1); 1758 } 1759 1760 /* 1761 * Assign the new dependency name. 1762 */ 1763 DBG_CALL(Dbg_file_deferred(lml, dyip->di_name, name)); 1764 dyip->di_flags |= FLG_DI_DEF_DONE; 1765 dyip->di_name = name; 1766 return (0); 1767 } 1768 1769 /* 1770 * Extract information for a dlopen() handle. 1771 */ 1772 static int 1773 dlinfo_core(void *handle, int request, void *p, Rt_map *clmp) 1774 { 1775 Conv_inv_buf_t inv_buf; 1776 char *handlename; 1777 Lm_list *lml = LIST(clmp); 1778 Rt_map *lmp = NULL; 1779 1780 /* 1781 * Determine whether a handle is provided. A handle isn't needed for 1782 * all operations, but it is validated here for the initial diagnostic. 1783 */ 1784 if (handle == RTLD_SELF) { 1785 lmp = clmp; 1786 } else { 1787 Grp_hdl *ghp = (Grp_hdl *)handle; 1788 1789 if (hdl_validate(ghp)) 1790 lmp = ghp->gh_ownlmp; 1791 } 1792 if (lmp) { 1793 handlename = NAME(lmp); 1794 } else { 1795 (void) conv_invalid_val(&inv_buf, EC_NATPTR(handle), 0); 1796 handlename = inv_buf.buf; 1797 } 1798 1799 DBG_CALL(Dbg_dl_dlinfo(clmp, handlename, request, p)); 1800 1801 /* 1802 * Validate the request and return buffer. 1803 */ 1804 if ((request > RTLD_DI_MAX) || (p == NULL)) { 1805 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLVAL)); 1806 return (-1); 1807 } 1808 1809 /* 1810 * Return configuration cache name and address. 1811 */ 1812 if (request == RTLD_DI_CONFIGADDR) { 1813 Dl_info_t *dlip = (Dl_info_t *)p; 1814 1815 if ((config->c_name == NULL) || (config->c_bgn == 0) || 1816 (config->c_end == 0)) { 1817 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_NOCONFIG)); 1818 return (-1); 1819 } 1820 dlip->dli_fname = config->c_name; 1821 dlip->dli_fbase = (void *)config->c_bgn; 1822 return (0); 1823 } 1824 1825 /* 1826 * Return profiled object name (used by ldprof audit library). 1827 */ 1828 if (request == RTLD_DI_PROFILENAME) { 1829 if (profile_name == NULL) { 1830 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_NOPROFNAME)); 1831 return (-1); 1832 } 1833 1834 *(const char **)p = profile_name; 1835 return (0); 1836 } 1837 if (request == RTLD_DI_PROFILEOUT) { 1838 /* 1839 * If a profile destination directory hasn't been specified 1840 * provide a default. 1841 */ 1842 if (profile_out == NULL) 1843 profile_out = MSG_ORIG(MSG_PTH_VARTMP); 1844 1845 *(const char **)p = profile_out; 1846 return (0); 1847 } 1848 1849 /* 1850 * Obtain or establish a termination signal. 1851 */ 1852 if (request == RTLD_DI_GETSIGNAL) { 1853 *(int *)p = killsig; 1854 return (0); 1855 } 1856 1857 if (request == RTLD_DI_SETSIGNAL) { 1858 sigset_t set; 1859 int sig = *(int *)p; 1860 1861 /* 1862 * Determine whether the signal is in range. 1863 */ 1864 (void) sigfillset(&set); 1865 if (sigismember(&set, sig) != 1) { 1866 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_INVSIG), sig); 1867 return (-1); 1868 } 1869 1870 killsig = sig; 1871 return (0); 1872 } 1873 1874 /* 1875 * For any other request a link-map is required. Verify the handle. 1876 */ 1877 if (lmp == NULL) { 1878 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL), 1879 EC_NATPTR(handle)); 1880 return (-1); 1881 } 1882 1883 /* 1884 * Obtain the process arguments, environment and auxv. Note, as the 1885 * environment can be modified by the user (putenv(3c)), reinitialize 1886 * the environment pointer on each request. 1887 */ 1888 if (request == RTLD_DI_ARGSINFO) { 1889 Dl_argsinfo_t *aip = (Dl_argsinfo_t *)p; 1890 Lm_list *lml = LIST(lmp); 1891 1892 *aip = argsinfo; 1893 if (lml->lm_flags & LML_FLG_ENVIRON) 1894 aip->dla_envp = *(lml->lm_environ); 1895 1896 return (0); 1897 } 1898 1899 /* 1900 * Return Lmid_t of the Link-Map list that the specified object is 1901 * loaded on. 1902 */ 1903 if (request == RTLD_DI_LMID) { 1904 *(Lmid_t *)p = get_linkmap_id(LIST(lmp)); 1905 return (0); 1906 } 1907 1908 /* 1909 * Return a pointer to the Link-Map structure associated with the 1910 * specified object. 1911 */ 1912 if (request == RTLD_DI_LINKMAP) { 1913 *(Link_map **)p = (Link_map *)lmp; 1914 return (0); 1915 } 1916 1917 /* 1918 * Return search path information, or the size of the buffer required 1919 * to store the information. 1920 */ 1921 if ((request == RTLD_DI_SERINFO) || (request == RTLD_DI_SERINFOSIZE)) { 1922 Spath_desc sd = { search_rules, NULL, 0 }; 1923 Pdesc *pdp; 1924 Dl_serinfo_t *info; 1925 Dl_serpath_t *path; 1926 char *strs; 1927 size_t size = sizeof (Dl_serinfo_t); 1928 uint_t cnt = 0; 1929 1930 info = (Dl_serinfo_t *)p; 1931 path = &info->dls_serpath[0]; 1932 strs = (char *)&info->dls_serpath[info->dls_cnt]; 1933 1934 /* 1935 * Traverse search path entries for this object. 1936 */ 1937 while ((pdp = get_next_dir(&sd, lmp, 0)) != NULL) { 1938 size_t _size; 1939 1940 if (pdp->pd_pname == NULL) 1941 continue; 1942 1943 /* 1944 * If configuration information exists, it's possible 1945 * this path has been identified as non-existent, if so 1946 * ignore it. 1947 */ 1948 if (pdp->pd_info) { 1949 Rtc_obj *dobj = (Rtc_obj *)pdp->pd_info; 1950 if (dobj->co_flags & RTC_OBJ_NOEXIST) 1951 continue; 1952 } 1953 1954 /* 1955 * Keep track of search path count and total info size. 1956 */ 1957 if (cnt++) 1958 size += sizeof (Dl_serpath_t); 1959 _size = pdp->pd_plen + 1; 1960 size += _size; 1961 1962 if (request == RTLD_DI_SERINFOSIZE) 1963 continue; 1964 1965 /* 1966 * If we're filling in search path information, confirm 1967 * there's sufficient space. 1968 */ 1969 if (size > info->dls_size) { 1970 eprintf(lml, ERR_FATAL, 1971 MSG_INTL(MSG_ARG_SERSIZE), 1972 EC_OFF(info->dls_size)); 1973 return (-1); 1974 } 1975 if (cnt > info->dls_cnt) { 1976 eprintf(lml, ERR_FATAL, 1977 MSG_INTL(MSG_ARG_SERCNT), info->dls_cnt); 1978 return (-1); 1979 } 1980 1981 /* 1982 * Append the path to the information buffer. 1983 */ 1984 (void) strcpy(strs, pdp->pd_pname); 1985 path->dls_name = strs; 1986 path->dls_flags = (pdp->pd_flags & LA_SER_MASK); 1987 1988 strs = strs + _size; 1989 path++; 1990 } 1991 1992 /* 1993 * If we're here to size the search buffer fill it in. 1994 */ 1995 if (request == RTLD_DI_SERINFOSIZE) { 1996 info->dls_size = size; 1997 info->dls_cnt = cnt; 1998 } 1999 2000 return (0); 2001 } 2002 2003 /* 2004 * Return the origin of the object associated with this link-map. 2005 * Basically return the dirname(1) of the objects fullpath. 2006 */ 2007 if (request == RTLD_DI_ORIGIN) { 2008 char *str = (char *)p; 2009 2010 (void) strncpy(str, ORIGNAME(lmp), DIRSZ(lmp)); 2011 str += DIRSZ(lmp); 2012 *str = '\0'; 2013 2014 return (0); 2015 } 2016 2017 /* 2018 * Return the number of object mappings, or the mapping information for 2019 * this object. 2020 */ 2021 if (request == RTLD_DI_MMAPCNT) { 2022 uint_t *cnt = (uint_t *)p; 2023 2024 *cnt = MMAPCNT(lmp); 2025 return (0); 2026 } 2027 if (request == RTLD_DI_MMAPS) { 2028 Dl_mapinfo_t *mip = (Dl_mapinfo_t *)p; 2029 2030 if (mip->dlm_acnt && mip->dlm_maps) { 2031 uint_t cnt = 0; 2032 2033 while ((cnt < mip->dlm_acnt) && (cnt < MMAPCNT(lmp))) { 2034 mip->dlm_maps[cnt] = MMAPS(lmp)[cnt]; 2035 cnt++; 2036 } 2037 mip->dlm_rcnt = cnt; 2038 } 2039 return (0); 2040 } 2041 2042 /* 2043 * Assign a new dependency name to a deferred dependency. 2044 */ 2045 if ((request == RTLD_DI_DEFERRED) || 2046 (request == RTLD_DI_DEFERRED_SYM)) { 2047 Dl_definfo_t *dfip = (Dl_definfo_t *)p; 2048 Dyninfo *dyip; 2049 const char *dname, *rname; 2050 2051 /* 2052 * Verify the names. 2053 */ 2054 if ((dfip->dld_refname == NULL) || 2055 (dfip->dld_depname == NULL)) { 2056 eprintf(LIST(clmp), ERR_FATAL, 2057 MSG_INTL(MSG_ARG_ILLNAME)); 2058 return (-1); 2059 } 2060 2061 dname = dfip->dld_depname; 2062 rname = dfip->dld_refname; 2063 2064 /* 2065 * A deferred dependency can be determined by referencing a 2066 * symbol family member that is associated to the dependency, 2067 * or by looking for the dependency by its name. 2068 */ 2069 if (request == RTLD_DI_DEFERRED_SYM) { 2070 Slookup sl; 2071 Sresult sr; 2072 uint_t binfo; 2073 Syminfo *sip; 2074 2075 /* 2076 * Lookup the symbol in the associated object. 2077 */ 2078 SLOOKUP_INIT(sl, rname, lmp, lmp, ld_entry_cnt, 2079 elf_hash(rname), 0, 0, 0, LKUP_SYMNDX); 2080 SRESULT_INIT(sr, rname); 2081 if (sym_lookup_in_caller(clmp, &sl, &sr, 2082 &binfo) == NULL) { 2083 eprintf(LIST(clmp), ERR_FATAL, 2084 MSG_INTL(MSG_DEF_NOSYMFOUND), rname); 2085 return (-1); 2086 } 2087 2088 /* 2089 * Use the symbols index to reference the Syminfo entry 2090 * and thus find the associated dependency. 2091 */ 2092 if (sl.sl_rsymndx && ((sip = SYMINFO(clmp)) != NULL)) { 2093 /* LINTED */ 2094 sip = (Syminfo *)((char *)sip + 2095 (sl.sl_rsymndx * SYMINENT(lmp))); 2096 2097 if ((sip->si_flags & SYMINFO_FLG_DEFERRED) && 2098 (sip->si_boundto < SYMINFO_BT_LOWRESERVE) && 2099 ((dyip = DYNINFO(lmp)) != NULL)) { 2100 dyip += sip->si_boundto; 2101 2102 if (!(dyip->di_flags & FLG_DI_IGNORE)) 2103 return (set_def_need(lml, 2104 dyip, dname)); 2105 } 2106 } 2107 2108 /* 2109 * No deferred symbol found. 2110 */ 2111 eprintf(LIST(clmp), ERR_FATAL, 2112 MSG_INTL(MSG_DEF_NOSYMFOUND), rname); 2113 return (-1); 2114 2115 } else { 2116 Dyn *dyn; 2117 2118 /* 2119 * Using the target objects dependency information, find 2120 * the associated deferred dependency. 2121 */ 2122 for (dyn = DYN(lmp), dyip = DYNINFO(lmp); 2123 !(dyip->di_flags & FLG_DI_IGNORE); dyn++, dyip++) { 2124 const char *oname; 2125 2126 if ((dyip->di_flags & FLG_DI_DEFERRED) == 0) 2127 continue; 2128 2129 if (strcmp(rname, dyip->di_name) == 0) 2130 return (set_def_need(lml, dyip, dname)); 2131 2132 /* 2133 * If this dependency name has been changed by 2134 * a previous dlinfo(), check the original 2135 * dynamic entry string. The user might be 2136 * attempting to re-change an entry using the 2137 * original name as the reference. 2138 */ 2139 if ((dyip->di_flags & FLG_DI_DEF_DONE) == 0) 2140 continue; 2141 2142 oname = STRTAB(lmp) + dyn->d_un.d_val; 2143 if (strcmp(rname, oname) == 0) 2144 return (set_def_need(lml, dyip, dname)); 2145 } 2146 2147 /* 2148 * No deferred dependency found. 2149 */ 2150 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_DEF_NODEPFOUND), 2151 rname); 2152 return (-1); 2153 } 2154 } 2155 return (0); 2156 } 2157 2158 #pragma weak _dlinfo = dlinfo 2159 2160 /* 2161 * External entry for dlinfo(3dl). 2162 */ 2163 int 2164 dlinfo(void *handle, int request, void *p) 2165 { 2166 int error, entry; 2167 Rt_map *clmp; 2168 2169 entry = enter(0); 2170 2171 clmp = _caller(caller(), CL_EXECDEF); 2172 2173 error = dlinfo_core(handle, request, p, clmp); 2174 2175 if (entry) 2176 leave(LIST(clmp), 0); 2177 return (error); 2178 } 2179 2180 /* 2181 * GNU defined function to iterate through the program headers for all 2182 * currently loaded dynamic objects. The caller supplies a callback function 2183 * which is called for each object. 2184 * 2185 * entry: 2186 * callback - Callback function to call. The arguments to the callback 2187 * function are: 2188 * info - Address of dl_phdr_info structure 2189 * size - sizeof (struct dl_phdr_info) 2190 * data - Caller supplied value. 2191 * data - Value supplied by caller, which is passed to callback without 2192 * examination. 2193 * 2194 * exit: 2195 * callback is called for each dynamic ELF object in the process address 2196 * space, halting when a non-zero value is returned, or when the last 2197 * object has been processed. The return value from the last call 2198 * to callback is returned. 2199 * 2200 * note: 2201 * The Linux implementation has added additional fields to the 2202 * dl_phdr_info structure over time. The callback function is 2203 * supposed to use the size field to determine which fields are 2204 * present, and to avoid attempts to access non-existent fields. 2205 * We have added those fields that are compatible with Solaris, and 2206 * which are used by GNU C++ (g++) runtime exception handling support. 2207 * 2208 * note: 2209 * We issue a callback for every ELF object mapped into the process 2210 * address space at the time this routine is entered. These callbacks 2211 * are arbitrary functions that can do anything, including possibly 2212 * causing new objects to be mapped into the process, or unmapped. 2213 * This complicates matters: 2214 * 2215 * - Adding new objects can cause the alists to be reallocated 2216 * or for contents to move. This can happen explicitly via 2217 * dlopen(), or implicitly via lazy loading. One might consider 2218 * simply banning dlopen from a callback, but lazy loading must 2219 * be allowed, in which case there's no reason to ban dlopen(). 2220 * 2221 * - Removing objects can leave us holding references to freed 2222 * memory that must not be accessed, and can cause the list 2223 * items to move in a way that would cause us to miss reporting 2224 * one, or double report others. 2225 * 2226 * - We cannot allocate memory to build a separate data structure, 2227 * because the interface to dl_iterate_phdr() does not have a 2228 * way to communicate allocation errors back to the caller. 2229 * Even if we could, it would be difficult to do so efficiently. 2230 * 2231 * - It is possible for dl_iterate_phdr() to be called recursively 2232 * from a callback, and there is no way for us to detect or manage 2233 * this effectively, particularly as the user might use longjmp() 2234 * to skip past us on return. Hence, we must be reentrant 2235 * (stateless), further precluding the option of building a 2236 * separate data structure. 2237 * 2238 * Despite these constraints, we are able to traverse the link-map 2239 * lists safely: 2240 * 2241 * - Once interposer (preload) objects have been processed at 2242 * startup, we know that new objects are always placed at the 2243 * end of the list. Hence, if we are reading a list when that 2244 * happens, the new object will not alter the part of the list 2245 * that we've already processed. 2246 * 2247 * - The alist _TRAVERSE macros recalculate the address of the 2248 * current item from scratch on each iteration, rather than 2249 * incrementing a pointer. Hence, alist additions that occur 2250 * in mid-traverse will not cause confusion. 2251 * 2252 * There is one limitation: We cannot continue operation if an object 2253 * is removed from the process from within a callback. We detect when 2254 * this happens and return immediately with a -1 return value. 2255 * 2256 * note: 2257 * As currently implemented, if a callback causes an object to be loaded, 2258 * that object may or may not be reported by the current invocation of 2259 * dl_iterate_phdr(), based on whether or not we have already processed 2260 * the link-map list that receives it. If we want to prevent this, it 2261 * can be done efficiently by associating the current value of cnt_map 2262 * with each new Rt_map entered into the system. Then this function can 2263 * use that to detect and skip new objects that enter the system in 2264 * mid-iteration. However, the Linux documentation is ambiguous on whether 2265 * this is necessary, and it does not appear to matter in practice. 2266 * We have therefore chosen not to do so at this time. 2267 */ 2268 int 2269 dl_iterate_phdr(int (*callback)(struct dl_phdr_info *, size_t, void *), 2270 void *data) 2271 { 2272 struct dl_phdr_info info; 2273 u_longlong_t l_cnt_map = cnt_map; 2274 u_longlong_t l_cnt_unmap = cnt_unmap; 2275 Lm_list *lml, *clml; 2276 Lm_cntl *lmc; 2277 Rt_map *lmp, *clmp; 2278 Aliste idx1, idx2; 2279 Ehdr *ehdr; 2280 int ret = 0; 2281 int entry; 2282 2283 entry = enter(0); 2284 clmp = _caller(caller(), CL_EXECDEF); 2285 clml = LIST(clmp); 2286 2287 DBG_CALL(Dbg_dl_iphdr_enter(clmp, cnt_map, cnt_unmap)); 2288 2289 /* Issue a callback for each ELF object in the process */ 2290 for (APLIST_TRAVERSE(dynlm_list, idx1, lml)) { 2291 for (ALIST_TRAVERSE(lml->lm_lists, idx2, lmc)) { 2292 for (lmp = lmc->lc_head; lmp; lmp = NEXT_RT_MAP(lmp)) { 2293 #if defined(_sparc) && !defined(_LP64) 2294 /* 2295 * On 32-bit sparc, the possibility exists that 2296 * this object is not ELF. 2297 */ 2298 if (THIS_IS_NOT_ELF(lmp)) 2299 continue; 2300 #endif 2301 /* Prepare the object information structure */ 2302 ehdr = (Ehdr *) ADDR(lmp); 2303 info.dlpi_addr = (ehdr->e_type == ET_EXEC) ? 2304 0 : ADDR(lmp); 2305 info.dlpi_name = lmp->rt_pathname; 2306 info.dlpi_phdr = (Phdr *) 2307 (ADDR(lmp) + ehdr->e_phoff); 2308 info.dlpi_phnum = ehdr->e_phnum; 2309 info.dlpi_adds = cnt_map; 2310 info.dlpi_subs = cnt_unmap; 2311 2312 /* Issue the callback */ 2313 DBG_CALL(Dbg_dl_iphdr_callback(clml, &info)); 2314 leave(clml, thr_flg_reenter); 2315 ret = (* callback)(&info, sizeof (info), data); 2316 (void) enter(thr_flg_reenter); 2317 2318 /* Return immediately on non-zero result */ 2319 if (ret != 0) 2320 goto done; 2321 2322 /* Adapt to object mapping changes */ 2323 if ((cnt_map == l_cnt_map) && 2324 (cnt_unmap == l_cnt_unmap)) 2325 continue; 2326 2327 DBG_CALL(Dbg_dl_iphdr_mapchange(clml, cnt_map, 2328 cnt_unmap)); 2329 2330 /* Stop if an object was unmapped */ 2331 if (cnt_unmap == l_cnt_unmap) { 2332 l_cnt_map = cnt_map; 2333 continue; 2334 } 2335 2336 ret = -1; 2337 DBG_CALL(Dbg_dl_iphdr_unmap_ret(clml)); 2338 goto done; 2339 } 2340 } 2341 } 2342 2343 done: 2344 if (entry) 2345 leave(LIST(clmp), 0); 2346 return (ret); 2347 } 2348