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, error; 1546 Rt_map *clmp, *almp; 1547 1548 entry = enter(0); 1549 1550 clmp = _caller(caller(), CL_EXECDEF); 1551 1552 DBG_CALL(Dbg_dl_dladdr(clmp, addr)); 1553 1554 /* 1555 * Use our calling technique to determine what object is associated 1556 * with the supplied address. If a caller can't be determined, 1557 * indicate the failure. 1558 */ 1559 if ((almp = _caller(addr, CL_NONE)) == NULL) { 1560 eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_INVADDR), 1561 EC_NATPTR(addr)); 1562 error = 0; 1563 } else { 1564 dladdr_core(almp, addr, dlip, 0, 0); 1565 error = 1; 1566 } 1567 1568 if (entry) 1569 leave(0, 0); 1570 return (error); 1571 } 1572 1573 #pragma weak _dladdr1 = dladdr1 1574 1575 int 1576 dladdr1(void *addr, Dl_info_t *dlip, void **info, int flags) 1577 { 1578 int entry, ret = 1; 1579 Rt_map *clmp, *almp; 1580 Lm_list *clml; 1581 1582 entry = enter(0); 1583 1584 clmp = _caller(caller(), CL_EXECDEF); 1585 clml = LIST(clmp); 1586 1587 DBG_CALL(Dbg_dl_dladdr(clmp, addr)); 1588 1589 /* 1590 * Validate any flags. 1591 */ 1592 if (flags) { 1593 int request; 1594 1595 if (((request = (flags & RTLD_DL_MASK)) != RTLD_DL_SYMENT) && 1596 (request != RTLD_DL_LINKMAP)) { 1597 eprintf(clml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLFLAGS), 1598 flags); 1599 ret = 0; 1600 1601 } else if (info == NULL) { 1602 eprintf(clml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLINFO), 1603 flags); 1604 ret = 0; 1605 } 1606 } 1607 1608 /* 1609 * Use our calling technique to determine what object is associated 1610 * with the supplied address. If a caller can't be determined, 1611 * indicate the failure. 1612 */ 1613 if (ret) { 1614 if ((almp = _caller(addr, CL_NONE)) == NULL) { 1615 eprintf(clml, ERR_FATAL, MSG_INTL(MSG_ARG_INVADDR), 1616 EC_NATPTR(addr)); 1617 ret = 0; 1618 } else 1619 dladdr_core(almp, addr, dlip, info, flags); 1620 } 1621 1622 if (entry) 1623 leave(clml, 0); 1624 return (ret); 1625 } 1626 1627 /* 1628 * Core dldump activity. 1629 */ 1630 static int 1631 dldump_core(Rt_map *clmp, Rt_map *lmp, const char *ipath, const char *opath, 1632 int flags) 1633 { 1634 Lm_list *lml = LIST(clmp); 1635 Addr addr = 0; 1636 1637 /* 1638 * Verify any arguments first. 1639 */ 1640 if ((opath == NULL) || (opath[0] == '\0') || 1641 ((lmp == NULL) && (ipath[0] == '\0'))) { 1642 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLPATH)); 1643 return (1); 1644 } 1645 1646 /* 1647 * If an input file is specified make sure its one of our dependencies 1648 * on the main link-map list. Note, this has really all evolved for 1649 * crle(), which uses libcrle.so on an alternative link-map to trigger 1650 * dumping objects from the main link-map list. If we ever want to 1651 * dump objects from alternative link-maps, this model is going to 1652 * have to be revisited. 1653 */ 1654 if (lmp == NULL) { 1655 if ((lmp = is_so_loaded(&lml_main, ipath, NULL)) == NULL) { 1656 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_NOFILE), 1657 ipath); 1658 return (1); 1659 } 1660 if (FLAGS(lmp) & FLG_RT_ALTER) { 1661 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_ALTER), ipath); 1662 return (1); 1663 } 1664 if (FLAGS(lmp) & FLG_RT_NODUMP) { 1665 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_NODUMP), 1666 ipath); 1667 return (1); 1668 } 1669 } 1670 1671 /* 1672 * If the object being dump'ed isn't fixed identify its mapping. 1673 */ 1674 if (!(FLAGS(lmp) & FLG_RT_FIXED)) 1675 addr = ADDR(lmp); 1676 1677 /* 1678 * As rt_dldump() will effectively lazy load the necessary support 1679 * libraries, make sure ld.so.1 is initialized for plt relocations. 1680 */ 1681 if (elf_rtld_load() == 0) 1682 return (0); 1683 1684 /* 1685 * Dump the required image. 1686 */ 1687 return (rt_dldump(lmp, opath, flags, addr)); 1688 } 1689 1690 #pragma weak _dldump = dldump 1691 1692 /* 1693 * External entry for dldump(3c). Returns 0 on success, non-zero otherwise. 1694 */ 1695 int 1696 dldump(const char *ipath, const char *opath, int flags) 1697 { 1698 int error, entry; 1699 Rt_map *clmp, *lmp; 1700 1701 entry = enter(0); 1702 1703 clmp = _caller(caller(), CL_EXECDEF); 1704 1705 if (ipath) { 1706 lmp = NULL; 1707 } else { 1708 lmp = lml_main.lm_head; 1709 ipath = NAME(lmp); 1710 } 1711 1712 DBG_CALL(Dbg_dl_dldump(clmp, ipath, opath, flags)); 1713 1714 error = dldump_core(clmp, lmp, ipath, opath, flags); 1715 1716 if (entry) 1717 leave(LIST(clmp), 0); 1718 return (error); 1719 } 1720 1721 /* 1722 * get_linkmap_id() translates Lm_list * pointers to the Link_map id as used by 1723 * the rtld_db and dlmopen() interfaces. It checks to see if the Link_map is 1724 * one of the primary ones and if so returns it's special token: 1725 * LM_ID_BASE 1726 * LM_ID_LDSO 1727 * 1728 * If it's not one of the primary link_map id's it will instead returns a 1729 * pointer to the Lm_list structure which uniquely identifies the Link_map. 1730 */ 1731 Lmid_t 1732 get_linkmap_id(Lm_list *lml) 1733 { 1734 if (lml->lm_flags & LML_FLG_BASELM) 1735 return (LM_ID_BASE); 1736 if (lml->lm_flags & LML_FLG_RTLDLM) 1737 return (LM_ID_LDSO); 1738 1739 return ((Lmid_t)lml); 1740 } 1741 1742 /* 1743 * Set a new deferred dependency name. 1744 */ 1745 static int 1746 set_def_need(Lm_list *lml, Dyninfo *dyip, const char *name) 1747 { 1748 /* 1749 * If this dependency has already been established, then this dlinfo() 1750 * call is too late. 1751 */ 1752 if (dyip->di_info) { 1753 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_DEF_DEPLOADED), 1754 dyip->di_name); 1755 return (-1); 1756 } 1757 1758 /* 1759 * Assign the new dependency name. 1760 */ 1761 DBG_CALL(Dbg_file_deferred(lml, dyip->di_name, name)); 1762 dyip->di_flags |= FLG_DI_DEF_DONE; 1763 dyip->di_name = name; 1764 return (0); 1765 } 1766 1767 /* 1768 * Extract information for a dlopen() handle. 1769 */ 1770 static int 1771 dlinfo_core(void *handle, int request, void *p, Rt_map *clmp) 1772 { 1773 Conv_inv_buf_t inv_buf; 1774 char *handlename; 1775 Lm_list *lml = LIST(clmp); 1776 Rt_map *lmp = NULL; 1777 1778 /* 1779 * Determine whether a handle is provided. A handle isn't needed for 1780 * all operations, but it is validated here for the initial diagnostic. 1781 */ 1782 if (handle == RTLD_SELF) { 1783 lmp = clmp; 1784 } else { 1785 Grp_hdl *ghp = (Grp_hdl *)handle; 1786 1787 if (hdl_validate(ghp)) 1788 lmp = ghp->gh_ownlmp; 1789 } 1790 if (lmp) { 1791 handlename = NAME(lmp); 1792 } else { 1793 (void) conv_invalid_val(&inv_buf, EC_NATPTR(handle), 0); 1794 handlename = inv_buf.buf; 1795 } 1796 1797 DBG_CALL(Dbg_dl_dlinfo(clmp, handlename, request, p)); 1798 1799 /* 1800 * Validate the request and return buffer. 1801 */ 1802 if ((request > RTLD_DI_MAX) || (p == NULL)) { 1803 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLVAL)); 1804 return (-1); 1805 } 1806 1807 /* 1808 * Return configuration cache name and address. 1809 */ 1810 if (request == RTLD_DI_CONFIGADDR) { 1811 Dl_info_t *dlip = (Dl_info_t *)p; 1812 1813 if ((config->c_name == NULL) || (config->c_bgn == 0) || 1814 (config->c_end == 0)) { 1815 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_NOCONFIG)); 1816 return (-1); 1817 } 1818 dlip->dli_fname = config->c_name; 1819 dlip->dli_fbase = (void *)config->c_bgn; 1820 return (0); 1821 } 1822 1823 /* 1824 * Return profiled object name (used by ldprof audit library). 1825 */ 1826 if (request == RTLD_DI_PROFILENAME) { 1827 if (profile_name == NULL) { 1828 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_NOPROFNAME)); 1829 return (-1); 1830 } 1831 1832 *(const char **)p = profile_name; 1833 return (0); 1834 } 1835 if (request == RTLD_DI_PROFILEOUT) { 1836 /* 1837 * If a profile destination directory hasn't been specified 1838 * provide a default. 1839 */ 1840 if (profile_out == NULL) 1841 profile_out = MSG_ORIG(MSG_PTH_VARTMP); 1842 1843 *(const char **)p = profile_out; 1844 return (0); 1845 } 1846 1847 /* 1848 * Obtain or establish a termination signal. 1849 */ 1850 if (request == RTLD_DI_GETSIGNAL) { 1851 *(int *)p = killsig; 1852 return (0); 1853 } 1854 1855 if (request == RTLD_DI_SETSIGNAL) { 1856 sigset_t set; 1857 int sig = *(int *)p; 1858 1859 /* 1860 * Determine whether the signal is in range. 1861 */ 1862 (void) sigfillset(&set); 1863 if (sigismember(&set, sig) != 1) { 1864 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_INVSIG), sig); 1865 return (-1); 1866 } 1867 1868 killsig = sig; 1869 return (0); 1870 } 1871 1872 /* 1873 * For any other request a link-map is required. Verify the handle. 1874 */ 1875 if (lmp == NULL) { 1876 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL), 1877 EC_NATPTR(handle)); 1878 return (-1); 1879 } 1880 1881 /* 1882 * Obtain the process arguments, environment and auxv. Note, as the 1883 * environment can be modified by the user (putenv(3c)), reinitialize 1884 * the environment pointer on each request. 1885 */ 1886 if (request == RTLD_DI_ARGSINFO) { 1887 Dl_argsinfo_t *aip = (Dl_argsinfo_t *)p; 1888 Lm_list *lml = LIST(lmp); 1889 1890 *aip = argsinfo; 1891 if (lml->lm_flags & LML_FLG_ENVIRON) 1892 aip->dla_envp = *(lml->lm_environ); 1893 1894 return (0); 1895 } 1896 1897 /* 1898 * Return Lmid_t of the Link-Map list that the specified object is 1899 * loaded on. 1900 */ 1901 if (request == RTLD_DI_LMID) { 1902 *(Lmid_t *)p = get_linkmap_id(LIST(lmp)); 1903 return (0); 1904 } 1905 1906 /* 1907 * Return a pointer to the Link-Map structure associated with the 1908 * specified object. 1909 */ 1910 if (request == RTLD_DI_LINKMAP) { 1911 *(Link_map **)p = (Link_map *)lmp; 1912 return (0); 1913 } 1914 1915 /* 1916 * Return search path information, or the size of the buffer required 1917 * to store the information. 1918 */ 1919 if ((request == RTLD_DI_SERINFO) || (request == RTLD_DI_SERINFOSIZE)) { 1920 Spath_desc sd = { search_rules, NULL, 0 }; 1921 Pdesc *pdp; 1922 Dl_serinfo_t *info; 1923 Dl_serpath_t *path; 1924 char *strs; 1925 size_t size = sizeof (Dl_serinfo_t); 1926 uint_t cnt = 0; 1927 1928 info = (Dl_serinfo_t *)p; 1929 path = &info->dls_serpath[0]; 1930 strs = (char *)&info->dls_serpath[info->dls_cnt]; 1931 1932 /* 1933 * Traverse search path entries for this object. 1934 */ 1935 while ((pdp = get_next_dir(&sd, lmp, 0)) != NULL) { 1936 size_t _size; 1937 1938 if (pdp->pd_pname == NULL) 1939 continue; 1940 1941 /* 1942 * If configuration information exists, it's possible 1943 * this path has been identified as non-existent, if so 1944 * ignore it. 1945 */ 1946 if (pdp->pd_info) { 1947 Rtc_obj *dobj = (Rtc_obj *)pdp->pd_info; 1948 if (dobj->co_flags & RTC_OBJ_NOEXIST) 1949 continue; 1950 } 1951 1952 /* 1953 * Keep track of search path count and total info size. 1954 */ 1955 if (cnt++) 1956 size += sizeof (Dl_serpath_t); 1957 _size = pdp->pd_plen + 1; 1958 size += _size; 1959 1960 if (request == RTLD_DI_SERINFOSIZE) 1961 continue; 1962 1963 /* 1964 * If we're filling in search path information, confirm 1965 * there's sufficient space. 1966 */ 1967 if (size > info->dls_size) { 1968 eprintf(lml, ERR_FATAL, 1969 MSG_INTL(MSG_ARG_SERSIZE), 1970 EC_OFF(info->dls_size)); 1971 return (-1); 1972 } 1973 if (cnt > info->dls_cnt) { 1974 eprintf(lml, ERR_FATAL, 1975 MSG_INTL(MSG_ARG_SERCNT), info->dls_cnt); 1976 return (-1); 1977 } 1978 1979 /* 1980 * Append the path to the information buffer. 1981 */ 1982 (void) strcpy(strs, pdp->pd_pname); 1983 path->dls_name = strs; 1984 path->dls_flags = (pdp->pd_flags & LA_SER_MASK); 1985 1986 strs = strs + _size; 1987 path++; 1988 } 1989 1990 /* 1991 * If we're here to size the search buffer fill it in. 1992 */ 1993 if (request == RTLD_DI_SERINFOSIZE) { 1994 info->dls_size = size; 1995 info->dls_cnt = cnt; 1996 } 1997 1998 return (0); 1999 } 2000 2001 /* 2002 * Return the origin of the object associated with this link-map. 2003 * Basically return the dirname(1) of the objects fullpath. 2004 */ 2005 if (request == RTLD_DI_ORIGIN) { 2006 char *str = (char *)p; 2007 2008 (void) strncpy(str, ORIGNAME(lmp), DIRSZ(lmp)); 2009 str += DIRSZ(lmp); 2010 *str = '\0'; 2011 2012 return (0); 2013 } 2014 2015 /* 2016 * Return the number of object mappings, or the mapping information for 2017 * this object. 2018 */ 2019 if (request == RTLD_DI_MMAPCNT) { 2020 uint_t *cnt = (uint_t *)p; 2021 2022 *cnt = MMAPCNT(lmp); 2023 return (0); 2024 } 2025 if (request == RTLD_DI_MMAPS) { 2026 Dl_mapinfo_t *mip = (Dl_mapinfo_t *)p; 2027 2028 if (mip->dlm_acnt && mip->dlm_maps) { 2029 uint_t cnt = 0; 2030 2031 while ((cnt < mip->dlm_acnt) && (cnt < MMAPCNT(lmp))) { 2032 mip->dlm_maps[cnt] = MMAPS(lmp)[cnt]; 2033 cnt++; 2034 } 2035 mip->dlm_rcnt = cnt; 2036 } 2037 return (0); 2038 } 2039 2040 /* 2041 * Assign a new dependency name to a deferred dependency. 2042 */ 2043 if ((request == RTLD_DI_DEFERRED) || 2044 (request == RTLD_DI_DEFERRED_SYM)) { 2045 Dl_definfo_t *dfip = (Dl_definfo_t *)p; 2046 Dyninfo *dyip; 2047 const char *dname, *rname; 2048 2049 /* 2050 * Verify the names. 2051 */ 2052 if ((dfip->dld_refname == NULL) || 2053 (dfip->dld_depname == NULL)) { 2054 eprintf(LIST(clmp), ERR_FATAL, 2055 MSG_INTL(MSG_ARG_ILLNAME)); 2056 return (-1); 2057 } 2058 2059 dname = dfip->dld_depname; 2060 rname = dfip->dld_refname; 2061 2062 /* 2063 * A deferred dependency can be determined by referencing a 2064 * symbol family member that is associated to the dependency, 2065 * or by looking for the dependency by its name. 2066 */ 2067 if (request == RTLD_DI_DEFERRED_SYM) { 2068 Slookup sl; 2069 Sresult sr; 2070 uint_t binfo; 2071 Syminfo *sip; 2072 2073 /* 2074 * Lookup the symbol in the associated object. 2075 */ 2076 SLOOKUP_INIT(sl, rname, lmp, lmp, ld_entry_cnt, 2077 elf_hash(rname), 0, 0, 0, LKUP_SYMNDX); 2078 SRESULT_INIT(sr, rname); 2079 if (sym_lookup_in_caller(clmp, &sl, &sr, 2080 &binfo) == NULL) { 2081 eprintf(LIST(clmp), ERR_FATAL, 2082 MSG_INTL(MSG_DEF_NOSYMFOUND), rname); 2083 return (-1); 2084 } 2085 2086 /* 2087 * Use the symbols index to reference the Syminfo entry 2088 * and thus find the associated dependency. 2089 */ 2090 if (sl.sl_rsymndx && ((sip = SYMINFO(clmp)) != NULL)) { 2091 /* LINTED */ 2092 sip = (Syminfo *)((char *)sip + 2093 (sl.sl_rsymndx * SYMINENT(lmp))); 2094 2095 if ((sip->si_flags & SYMINFO_FLG_DEFERRED) && 2096 (sip->si_boundto < SYMINFO_BT_LOWRESERVE) && 2097 ((dyip = DYNINFO(lmp)) != NULL)) { 2098 dyip += sip->si_boundto; 2099 2100 if (!(dyip->di_flags & FLG_DI_IGNORE)) 2101 return (set_def_need(lml, 2102 dyip, dname)); 2103 } 2104 } 2105 2106 /* 2107 * No deferred symbol found. 2108 */ 2109 eprintf(LIST(clmp), ERR_FATAL, 2110 MSG_INTL(MSG_DEF_NOSYMFOUND), rname); 2111 return (-1); 2112 2113 } else { 2114 Dyn *dyn; 2115 2116 /* 2117 * Using the target objects dependency information, find 2118 * the associated deferred dependency. 2119 */ 2120 for (dyn = DYN(lmp), dyip = DYNINFO(lmp); 2121 !(dyip->di_flags & FLG_DI_IGNORE); dyn++, dyip++) { 2122 const char *oname; 2123 2124 if ((dyip->di_flags & FLG_DI_DEFERRED) == 0) 2125 continue; 2126 2127 if (strcmp(rname, dyip->di_name) == 0) 2128 return (set_def_need(lml, dyip, dname)); 2129 2130 /* 2131 * If this dependency name has been changed by 2132 * a previous dlinfo(), check the original 2133 * dynamic entry string. The user might be 2134 * attempting to re-change an entry using the 2135 * original name as the reference. 2136 */ 2137 if ((dyip->di_flags & FLG_DI_DEF_DONE) == 0) 2138 continue; 2139 2140 oname = STRTAB(lmp) + dyn->d_un.d_val; 2141 if (strcmp(rname, oname) == 0) 2142 return (set_def_need(lml, dyip, dname)); 2143 } 2144 2145 /* 2146 * No deferred dependency found. 2147 */ 2148 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_DEF_NODEPFOUND), 2149 rname); 2150 return (-1); 2151 } 2152 } 2153 return (0); 2154 } 2155 2156 #pragma weak _dlinfo = dlinfo 2157 2158 /* 2159 * External entry for dlinfo(3dl). 2160 */ 2161 int 2162 dlinfo(void *handle, int request, void *p) 2163 { 2164 int error, entry; 2165 Rt_map *clmp; 2166 2167 entry = enter(0); 2168 2169 clmp = _caller(caller(), CL_EXECDEF); 2170 2171 error = dlinfo_core(handle, request, p, clmp); 2172 2173 if (entry) 2174 leave(LIST(clmp), 0); 2175 return (error); 2176 } 2177 2178 /* 2179 * GNU defined function to iterate through the program headers for all 2180 * currently loaded dynamic objects. The caller supplies a callback function 2181 * which is called for each object. 2182 * 2183 * entry: 2184 * callback - Callback function to call. The arguments to the callback 2185 * function are: 2186 * info - Address of dl_phdr_info structure 2187 * size - sizeof (struct dl_phdr_info) 2188 * data - Caller supplied value. 2189 * data - Value supplied by caller, which is passed to callback without 2190 * examination. 2191 * 2192 * exit: 2193 * callback is called for each dynamic ELF object in the process address 2194 * space, halting when a non-zero value is returned, or when the last 2195 * object has been processed. The return value from the last call 2196 * to callback is returned. 2197 * 2198 * note: 2199 * The Linux implementation has added additional fields to the 2200 * dl_phdr_info structure over time. The callback function is 2201 * supposed to use the size field to determine which fields are 2202 * present, and to avoid attempts to access non-existent fields. 2203 * We have added those fields that are compatible with Solaris, and 2204 * which are used by GNU C++ (g++) runtime exception handling support. 2205 * 2206 * note: 2207 * We issue a callback for every ELF object mapped into the process 2208 * address space at the time this routine is entered. These callbacks 2209 * are arbitrary functions that can do anything, including possibly 2210 * causing new objects to be mapped into the process, or unmapped. 2211 * This complicates matters: 2212 * 2213 * - Adding new objects can cause the alists to be reallocated 2214 * or for contents to move. This can happen explicitly via 2215 * dlopen(), or implicitly via lazy loading. One might consider 2216 * simply banning dlopen from a callback, but lazy loading must 2217 * be allowed, in which case there's no reason to ban dlopen(). 2218 * 2219 * - Removing objects can leave us holding references to freed 2220 * memory that must not be accessed, and can cause the list 2221 * items to move in a way that would cause us to miss reporting 2222 * one, or double report others. 2223 * 2224 * - We cannot allocate memory to build a separate data structure, 2225 * because the interface to dl_iterate_phdr() does not have a 2226 * way to communicate allocation errors back to the caller. 2227 * Even if we could, it would be difficult to do so efficiently. 2228 * 2229 * - It is possible for dl_iterate_phdr() to be called recursively 2230 * from a callback, and there is no way for us to detect or manage 2231 * this effectively, particularly as the user might use longjmp() 2232 * to skip past us on return. Hence, we must be reentrant 2233 * (stateless), further precluding the option of building a 2234 * separate data structure. 2235 * 2236 * Despite these constraints, we are able to traverse the link-map 2237 * lists safely: 2238 * 2239 * - Once interposer (preload) objects have been processed at 2240 * startup, we know that new objects are always placed at the 2241 * end of the list. Hence, if we are reading a list when that 2242 * happens, the new object will not alter the part of the list 2243 * that we've already processed. 2244 * 2245 * - The alist _TRAVERSE macros recalculate the address of the 2246 * current item from scratch on each iteration, rather than 2247 * incrementing a pointer. Hence, alist additions that occur 2248 * in mid-traverse will not cause confusion. 2249 * 2250 * There is one limitation: We cannot continue operation if an object 2251 * is removed from the process from within a callback. We detect when 2252 * this happens and return immediately with a -1 return value. 2253 * 2254 * note: 2255 * As currently implemented, if a callback causes an object to be loaded, 2256 * that object may or may not be reported by the current invocation of 2257 * dl_iterate_phdr(), based on whether or not we have already processed 2258 * the link-map list that receives it. If we want to prevent this, it 2259 * can be done efficiently by associating the current value of cnt_map 2260 * with each new Rt_map entered into the system. Then this function can 2261 * use that to detect and skip new objects that enter the system in 2262 * mid-iteration. However, the Linux documentation is ambiguous on whether 2263 * this is necessary, and it does not appear to matter in practice. 2264 * We have therefore chosen not to do so at this time. 2265 */ 2266 int 2267 dl_iterate_phdr(int (*callback)(struct dl_phdr_info *, size_t, void *), 2268 void *data) 2269 { 2270 struct dl_phdr_info info; 2271 u_longlong_t l_cnt_map = cnt_map; 2272 u_longlong_t l_cnt_unmap = cnt_unmap; 2273 Lm_list *lml, *clml; 2274 Lm_cntl *lmc; 2275 Rt_map *lmp, *clmp; 2276 Aliste idx1, idx2; 2277 Ehdr *ehdr; 2278 int ret = 0; 2279 int entry; 2280 2281 entry = enter(0); 2282 clmp = _caller(caller(), CL_EXECDEF); 2283 clml = LIST(clmp); 2284 2285 DBG_CALL(Dbg_dl_iphdr_enter(clmp, cnt_map, cnt_unmap)); 2286 2287 /* Issue a callback for each ELF object in the process */ 2288 for (APLIST_TRAVERSE(dynlm_list, idx1, lml)) { 2289 for (ALIST_TRAVERSE(lml->lm_lists, idx2, lmc)) { 2290 for (lmp = lmc->lc_head; lmp; lmp = NEXT_RT_MAP(lmp)) { 2291 #if defined(_sparc) && !defined(_LP64) 2292 /* 2293 * On 32-bit sparc, the possibility exists that 2294 * this object is not ELF. 2295 */ 2296 if (THIS_IS_NOT_ELF(lmp)) 2297 continue; 2298 #endif 2299 /* Prepare the object information structure */ 2300 ehdr = (Ehdr *) ADDR(lmp); 2301 info.dlpi_addr = (ehdr->e_type == ET_EXEC) ? 2302 0 : ADDR(lmp); 2303 info.dlpi_name = lmp->rt_pathname; 2304 info.dlpi_phdr = (Phdr *) 2305 (ADDR(lmp) + ehdr->e_phoff); 2306 info.dlpi_phnum = ehdr->e_phnum; 2307 info.dlpi_adds = cnt_map; 2308 info.dlpi_subs = cnt_unmap; 2309 2310 /* Issue the callback */ 2311 DBG_CALL(Dbg_dl_iphdr_callback(clml, &info)); 2312 leave(clml, thr_flg_reenter); 2313 ret = (* callback)(&info, sizeof (info), data); 2314 (void) enter(thr_flg_reenter); 2315 2316 /* Return immediately on non-zero result */ 2317 if (ret != 0) 2318 goto done; 2319 2320 /* Adapt to object mapping changes */ 2321 if ((cnt_map == l_cnt_map) && 2322 (cnt_unmap == l_cnt_unmap)) 2323 continue; 2324 2325 DBG_CALL(Dbg_dl_iphdr_mapchange(clml, cnt_map, 2326 cnt_unmap)); 2327 2328 /* Stop if an object was unmapped */ 2329 if (cnt_unmap == l_cnt_unmap) { 2330 l_cnt_map = cnt_map; 2331 continue; 2332 } 2333 2334 ret = -1; 2335 DBG_CALL(Dbg_dl_iphdr_unmap_ret(clml)); 2336 goto done; 2337 } 2338 } 2339 } 2340 2341 done: 2342 if (entry) 2343 leave(LIST(clmp), 0); 2344 return (ret); 2345 } 2346