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 2008 Sun Microsystems, Inc. All rights reserved. 27 * Use is subject to license terms. 28 */ 29 #pragma ident "%Z%%M% %I% %E% SMI" 30 31 /* 32 * Utility routines for run-time linker. some are duplicated here from libc 33 * (with different names) to avoid name space collisions. 34 */ 35 #include "_synonyms.h" 36 #include <stdio.h> 37 #include <sys/types.h> 38 #include <sys/mman.h> 39 #include <sys/lwp.h> 40 #include <sys/debug.h> 41 #include <stdarg.h> 42 #include <fcntl.h> 43 #include <string.h> 44 #include <ctype.h> 45 #include <dlfcn.h> 46 #include <unistd.h> 47 #include <stdlib.h> 48 #include <sys/auxv.h> 49 #include <debug.h> 50 #include <conv.h> 51 #include "_rtld.h" 52 #include "_audit.h" 53 #include "_elf.h" 54 #include "msg.h" 55 56 static int ld_flags_env(const char *, Word *, Word *, uint_t, int); 57 58 /* 59 * All error messages go through eprintf(). During process initialization these 60 * messages should be directed to the standard error, however once control has 61 * been passed to the applications code these messages should be stored in an 62 * internal buffer for use with dlerror(). Note, fatal error conditions that 63 * may occur while running the application will still cause a standard error 64 * message, see rtldexit() in this file for details. 65 * The `application' flag serves to indicate the transition between process 66 * initialization and when the applications code is running. 67 */ 68 69 /* 70 * Null function used as place where a debugger can set a breakpoint. 71 */ 72 void 73 rtld_db_dlactivity(Lm_list *lml) 74 { 75 DBG_CALL(Dbg_util_dbnotify(lml, r_debug.rtd_rdebug.r_rdevent, 76 r_debug.rtd_rdebug.r_state)); 77 } 78 79 /* 80 * Null function used as place where debugger can set a pre .init 81 * processing breakpoint. 82 */ 83 void 84 rtld_db_preinit(Lm_list *lml) 85 { 86 DBG_CALL(Dbg_util_dbnotify(lml, r_debug.rtd_rdebug.r_rdevent, 87 r_debug.rtd_rdebug.r_state)); 88 } 89 90 /* 91 * Null function used as place where debugger can set a post .init 92 * processing breakpoint. 93 */ 94 void 95 rtld_db_postinit(Lm_list *lml) 96 { 97 DBG_CALL(Dbg_util_dbnotify(lml, r_debug.rtd_rdebug.r_rdevent, 98 r_debug.rtd_rdebug.r_state)); 99 } 100 101 /* 102 * Debugger Event Notification 103 * 104 * This function centralizes all debugger event notification (ala rtld_db). 105 * 106 * There's a simple intent, focused on insuring the primary link-map control 107 * list (or each link-map list) is consistent, and the indication that objects 108 * have been added or deleted from this list. Although an RD_ADD and RD_DELETE 109 * event are posted for each of these, most debuggers don't care, as their 110 * view is that these events simply convey an "inconsistent" state. 111 * 112 * We also don't want to trigger multiple RD_ADD/RD_DELETE events any time we 113 * enter ld.so.1. 114 * 115 * With auditors, we may be in the process of relocating a collection of 116 * objects, and will leave() ld.so.1 to call the auditor. At this point we 117 * must indicate an RD_CONSISTENT event, but librtld_db will not report an 118 * object to the debuggers until relocation processing has been completed on it. 119 * To allow for the collection of these objects that are pending relocation, an 120 * RD_ADD event is set after completing a series of relocations on the primary 121 * link-map control list. 122 * 123 * Set an RD_ADD/RD_DELETE event and indicate that an RD_CONSISTENT event is 124 * required later (LML_FLG_DBNOTIF): 125 * 126 * i the first time we add or delete an object to the primary link-map 127 * control list. 128 * ii the first time we move a secondary link-map control list to the primary 129 * link-map control list (effectively, this is like adding a group of 130 * objects to the primary link-map control list). 131 * 132 * Set an RD_CONSISTENT event when it is required (LML_FLG_DBNOTIF is set) and 133 * 134 * i each time we leave the runtime linker. 135 */ 136 void 137 rd_event(Lm_list *lml, rd_event_e event, r_state_e state) 138 { 139 void (*fptr)(Lm_list *); 140 141 switch (event) { 142 case RD_PREINIT: 143 fptr = rtld_db_preinit; 144 break; 145 case RD_POSTINIT: 146 fptr = rtld_db_postinit; 147 break; 148 case RD_DLACTIVITY: 149 switch (state) { 150 case RT_CONSISTENT: 151 lml->lm_flags &= ~LML_FLG_DBNOTIF; 152 153 /* 154 * Do we need to send a notification? 155 */ 156 if ((rtld_flags & RT_FL_DBNOTIF) == 0) 157 return; 158 rtld_flags &= ~RT_FL_DBNOTIF; 159 break; 160 case RT_ADD: 161 case RT_DELETE: 162 lml->lm_flags |= LML_FLG_DBNOTIF; 163 164 /* 165 * If we are already in an inconsistent state, no 166 * notification is required. 167 */ 168 if (rtld_flags & RT_FL_DBNOTIF) 169 return; 170 rtld_flags |= RT_FL_DBNOTIF; 171 break; 172 }; 173 fptr = rtld_db_dlactivity; 174 break; 175 default: 176 /* 177 * RD_NONE - do nothing 178 */ 179 break; 180 }; 181 182 /* 183 * Set event state and call 'notification' function. 184 * 185 * The debugging clients have previously been told about these 186 * notification functions and have set breakpoints on them if they 187 * are interested in the notification. 188 */ 189 r_debug.rtd_rdebug.r_state = state; 190 r_debug.rtd_rdebug.r_rdevent = event; 191 fptr(lml); 192 r_debug.rtd_rdebug.r_rdevent = RD_NONE; 193 } 194 195 #if defined(__sparc) || defined(__x86) 196 /* 197 * Stack Cleanup. 198 * 199 * This function is invoked to 'remove' arguments that were passed in on the 200 * stack. This is most likely if ld.so.1 was invoked directly. In that case 201 * we want to remove ld.so.1 as well as it's arguments from the argv[] array. 202 * Which means we then need to slide everything above it on the stack down 203 * accordingly. 204 * 205 * While the stack layout is platform specific - it just so happens that __x86, 206 * and __sparc platforms share the following initial stack layout. 207 * 208 * !_______________________! high addresses 209 * ! ! 210 * ! Information ! 211 * ! Block ! 212 * ! (size varies) ! 213 * !_______________________! 214 * ! 0 word ! 215 * !_______________________! 216 * ! Auxiliary ! 217 * ! vector ! 218 * ! 2 word entries ! 219 * ! ! 220 * !_______________________! 221 * ! 0 word ! 222 * !_______________________! 223 * ! Environment ! 224 * ! pointers ! 225 * ! ... ! 226 * ! (one word each) ! 227 * !_______________________! 228 * ! 0 word ! 229 * !_______________________! 230 * ! Argument ! low addresses 231 * ! pointers ! 232 * ! Argc words ! 233 * !_______________________! 234 * ! ! 235 * ! Argc ! 236 * !_______________________! 237 * ! ... ! 238 * 239 */ 240 static void 241 stack_cleanup(char **argv, char ***envp, auxv_t **auxv, int rmcnt) 242 { 243 int ndx; 244 long *argc; 245 char **oargv, **nargv; 246 char **oenvp, **nenvp; 247 auxv_t *oauxv, *nauxv; 248 249 /* 250 * Slide ARGV[] and update argc. The argv pointer remains the same, 251 * however slide the applications arguments over the arguments to 252 * ld.so.1. 253 */ 254 nargv = &argv[0]; 255 oargv = &argv[rmcnt]; 256 257 for (ndx = 0; oargv[ndx]; ndx++) 258 nargv[ndx] = oargv[ndx]; 259 nargv[ndx] = oargv[ndx]; 260 261 argc = (long *)((uintptr_t)argv - sizeof (long *)); 262 *argc -= rmcnt; 263 264 /* 265 * Slide ENVP[], and update the environment array pointer. 266 */ 267 ndx++; 268 nenvp = &nargv[ndx]; 269 oenvp = &oargv[ndx]; 270 *envp = nenvp; 271 272 for (ndx = 0; oenvp[ndx]; ndx++) 273 nenvp[ndx] = oenvp[ndx]; 274 nenvp[ndx] = oenvp[ndx]; 275 276 /* 277 * Slide AUXV[], and update the aux vector pointer. 278 */ 279 ndx++; 280 nauxv = (auxv_t *)&nenvp[ndx]; 281 oauxv = (auxv_t *)&oenvp[ndx]; 282 *auxv = nauxv; 283 284 for (ndx = 0; (oauxv[ndx].a_type != AT_NULL); ndx++) 285 nauxv[ndx] = oauxv[ndx]; 286 nauxv[ndx] = oauxv[ndx]; 287 } 288 #else 289 /* 290 * Verify that the above routine is appropriate for any new platforms. 291 */ 292 #error unsupported architecture! 293 #endif 294 295 /* 296 * The only command line argument recognized is -e, followed by a runtime 297 * linker environment variable. 298 */ 299 int 300 rtld_getopt(char **argv, char ***envp, auxv_t **auxv, Word *lmflags, 301 Word *lmtflags, int aout) 302 { 303 int ndx; 304 305 for (ndx = 1; argv[ndx]; ndx++) { 306 char *str; 307 308 if (argv[ndx][0] != '-') 309 break; 310 311 if (argv[ndx][1] == '\0') { 312 ndx++; 313 break; 314 } 315 316 if (argv[ndx][1] != 'e') 317 return (1); 318 319 if (argv[ndx][2] == '\0') { 320 ndx++; 321 if (argv[ndx] == NULL) 322 return (1); 323 str = argv[ndx]; 324 } else 325 str = &argv[ndx][2]; 326 327 /* 328 * If the environment variable starts with LD_, strip the LD_. 329 * Otherwise, take things as is. 330 */ 331 if ((str[0] == 'L') && (str[1] == 'D') && (str[2] == '_') && 332 (str[3] != '\0')) 333 str += 3; 334 if (ld_flags_env(str, lmflags, lmtflags, 0, aout) == 1) 335 return (1); 336 } 337 338 /* 339 * Make sure an object file has been specified. 340 */ 341 if (argv[ndx] == 0) 342 return (1); 343 344 /* 345 * Having gotten the arguments, clean ourselves off of the stack. 346 */ 347 stack_cleanup(argv, envp, auxv, ndx); 348 return (0); 349 } 350 351 /* 352 * Compare function for PathNode AVL tree. 353 */ 354 static int 355 pnavl_compare(const void *n1, const void *n2) 356 { 357 uint_t hash1, hash2; 358 const char *st1, *st2; 359 int rc; 360 361 hash1 = ((PathNode *)n1)->pn_hash; 362 hash2 = ((PathNode *)n2)->pn_hash; 363 364 if (hash1 > hash2) 365 return (1); 366 if (hash1 < hash2) 367 return (-1); 368 369 st1 = ((PathNode *)n1)->pn_name; 370 st2 = ((PathNode *)n2)->pn_name; 371 372 rc = strcmp(st1, st2); 373 if (rc > 0) 374 return (1); 375 if (rc < 0) 376 return (-1); 377 return (0); 378 } 379 380 /* 381 * Determine if a pathname has already been recorded on the full path name 382 * AVL tree. This tree maintains a node for each path name that ld.so.1 has 383 * successfully loaded. If the path name does not exist in this AVL tree, then 384 * the next insertion point is deposited in "where". This value can be used by 385 * fpavl_insert() to expedite the insertion. 386 */ 387 Rt_map * 388 fpavl_recorded(Lm_list *lml, const char *name, avl_index_t *where) 389 { 390 FullPathNode fpn, *fpnp; 391 avl_tree_t *avlt; 392 393 /* 394 * Create the avl tree if required. 395 */ 396 if ((avlt = lml->lm_fpavl) == NULL) { 397 if ((avlt = calloc(sizeof (avl_tree_t), 1)) == 0) 398 return (0); 399 avl_create(avlt, pnavl_compare, sizeof (FullPathNode), 400 SGSOFFSETOF(FullPathNode, fpn_node.pn_avl)); 401 lml->lm_fpavl = avlt; 402 } 403 404 fpn.fpn_node.pn_name = name; 405 fpn.fpn_node.pn_hash = sgs_str_hash(name); 406 407 if ((fpnp = avl_find(lml->lm_fpavl, &fpn, where)) == NULL) 408 return (NULL); 409 410 return (fpnp->fpn_lmp); 411 } 412 413 /* 414 * Insert a name into the FullPathNode AVL tree for the link-map list. The 415 * objects NAME() is the path that would have originally been searched for, and 416 * is therefore the name to associate with any "where" value. If the object has 417 * a different PATHNAME(), perhaps because it has resolved to a different file 418 * (see fullpath()), then this name will be recorded as a separate FullPathNode 419 * (see load_file()). 420 */ 421 int 422 fpavl_insert(Lm_list *lml, Rt_map *lmp, const char *name, avl_index_t where) 423 { 424 FullPathNode *fpnp; 425 426 if (where == 0) { 427 /* LINTED */ 428 Rt_map *_lmp = fpavl_recorded(lml, name, &where); 429 430 /* 431 * We better not get a hit now, we do not want duplicates in 432 * the tree. 433 */ 434 ASSERT(_lmp == 0); 435 } 436 437 /* 438 * Insert new node in tree. 439 */ 440 if ((fpnp = calloc(sizeof (FullPathNode), 1)) == 0) 441 return (0); 442 443 fpnp->fpn_node.pn_name = name; 444 fpnp->fpn_node.pn_hash = sgs_str_hash(name); 445 fpnp->fpn_lmp = lmp; 446 447 if (aplist_append(&FPNODE(lmp), fpnp, AL_CNT_FPNODE) == NULL) { 448 free(fpnp); 449 return (0); 450 } 451 452 ASSERT(lml->lm_fpavl != NULL); 453 avl_insert(lml->lm_fpavl, fpnp, where); 454 return (1); 455 } 456 457 /* 458 * Remove an object from the FullPath AVL tree. Note, this is called *before* 459 * the objects link-map is torn down (remove_so), which is where any NAME() and 460 * PATHNAME() strings will be deallocated. 461 */ 462 void 463 fpavl_remove(Rt_map *lmp) 464 { 465 FullPathNode *fpnp; 466 Aliste idx; 467 468 for (APLIST_TRAVERSE(FPNODE(lmp), idx, fpnp)) { 469 avl_remove(LIST(lmp)->lm_fpavl, fpnp); 470 free(fpnp); 471 } 472 free(FPNODE(lmp)); 473 FPNODE(lmp) = NULL; 474 } 475 476 /* 477 * Determine if a pathname has already been recorded on the not-found AVL tree. 478 * This tree maintains a node for each path name that ld.so.1 has explicitly 479 * inspected, but has failed to load during a single ld.so.1 operation. If the 480 * path name does not exist in this AVL tree, then the next insertion point is 481 * deposited in "where". This value can be used by nfavl_insert() to expedite 482 * the insertion. 483 */ 484 int 485 nfavl_recorded(const char *name, avl_index_t *where) 486 { 487 PathNode pn; 488 avl_tree_t *avlt; 489 490 /* 491 * Create the avl tree if required. 492 */ 493 if ((avlt = nfavl) == NULL) { 494 if ((avlt = calloc(sizeof (avl_tree_t), 1)) == 0) 495 return (0); 496 avl_create(avlt, pnavl_compare, sizeof (PathNode), 497 SGSOFFSETOF(PathNode, pn_avl)); 498 nfavl = avlt; 499 } 500 501 pn.pn_name = name; 502 pn.pn_hash = sgs_str_hash(name); 503 504 if (avl_find(avlt, &pn, where) == NULL) 505 return (0); 506 507 return (1); 508 } 509 510 /* 511 * Insert a name into the not-found AVL tree. 512 */ 513 void 514 nfavl_insert(const char *name, avl_index_t where) 515 { 516 PathNode *pnp; 517 518 if (where == 0) { 519 /* LINTED */ 520 int in_nfavl = nfavl_recorded(name, &where); 521 522 /* 523 * We better not get a hit now, we do not want duplicates in 524 * the tree. 525 */ 526 ASSERT(in_nfavl == 0); 527 } 528 529 /* 530 * Insert new node in tree. 531 */ 532 if ((pnp = calloc(sizeof (PathNode), 1)) != 0) { 533 pnp->pn_name = name; 534 pnp->pn_hash = sgs_str_hash(name); 535 avl_insert(nfavl, pnp, where); 536 } 537 } 538 539 /* 540 * Prior to calling an object, either via a .plt or through dlsym(), make sure 541 * its .init has fired. Through topological sorting, ld.so.1 attempts to fire 542 * init's in the correct order, however, this order is typically based on needed 543 * dependencies and non-lazy relocation bindings. Lazy relocations (.plts) can 544 * still occur and result in bindings that were not captured during topological 545 * sorting. This routine compensates for this lack of binding information, and 546 * provides for dynamic .init firing. 547 */ 548 void 549 is_dep_init(Rt_map *dlmp, Rt_map *clmp) 550 { 551 Rt_map **tobj; 552 553 /* 554 * If the caller is an auditor, and the destination isn't, then don't 555 * run any .inits (see comments in load_completion()). 556 */ 557 if ((LIST(clmp)->lm_flags & LML_FLG_NOAUDIT) && 558 (LIST(clmp) != LIST(dlmp))) 559 return; 560 561 if ((dlmp == clmp) || (rtld_flags & (RT_FL_BREADTH | RT_FL_INITFIRST))) 562 return; 563 564 if ((FLAGS(dlmp) & (FLG_RT_RELOCED | FLG_RT_INITDONE)) == 565 (FLG_RT_RELOCED | FLG_RT_INITDONE)) 566 return; 567 568 if ((FLAGS(dlmp) & (FLG_RT_RELOCED | FLG_RT_INITCALL)) == 569 (FLG_RT_RELOCED | FLG_RT_INITCALL)) { 570 DBG_CALL(Dbg_util_no_init(dlmp)); 571 return; 572 } 573 574 if ((tobj = calloc(2, sizeof (Rt_map *))) != NULL) { 575 tobj[0] = dlmp; 576 call_init(tobj, DBG_INIT_DYN); 577 } 578 } 579 580 /* 581 * In a threaded environment insure the thread responsible for loading an object 582 * has completed .init processing for that object before any new thread is 583 * allowed to access the object. This check is only valid with libthread 584 * TI_VERSION 2, where ld.so.1 implements locking through low level mutexes. 585 * 586 * When a new link-map is created, the thread that causes it to be loaded is 587 * identified by THREADID(dlmp). Compare this with the current thread to 588 * determine if it must be blocked. 589 * 590 * NOTE, there are a number of instances (typically only for .plt processing) 591 * where we must skip this test: 592 * 593 * . any thread id of 0 - threads that call thr_exit() may be in this state 594 * thus we can't deduce what tid they used to be. Also some of the 595 * lib/libthread worker threads have this id and must bind (to themselves 596 * or libc) for libthread to function. 597 * 598 * . libthread itself binds to libc, and as libthread is INITFIRST 599 * libc's .init can't have fired yet. Luckly libc's .init is not required 600 * by libthreads binding. 601 * 602 * . if the caller is an auditor, and the destination isn't, then don't 603 * block (see comments in load_completion()). 604 */ 605 void 606 is_dep_ready(Rt_map *dlmp, Rt_map *clmp, int what) 607 { 608 thread_t tid; 609 610 if ((LIST(clmp)->lm_flags & LML_FLG_NOAUDIT) && 611 (LIST(clmp) != LIST(dlmp))) 612 return; 613 614 if ((rtld_flags & RT_FL_CONCUR) && 615 ((FLAGS(dlmp) & FLG_RT_INITDONE) == 0) && 616 ((FLAGS(clmp) & FLG_RT_INITFRST) == 0) && 617 ((tid = rt_thr_self()) != 0) && (THREADID(dlmp) != tid)) { 618 while ((FLAGS(dlmp) & FLG_RT_INITDONE) == 0) { 619 FLAGS1(dlmp) |= FL1_RT_INITWAIT; 620 DBG_CALL(Dbg_util_wait(clmp, dlmp, what)); 621 (void) rt_cond_wait(CONDVAR(dlmp), &rtldlock); 622 } 623 } 624 } 625 626 /* 627 * Execute .{preinit|init|fini}array sections 628 */ 629 void 630 call_array(Addr *array, uint_t arraysz, Rt_map *lmp, Word shtype) 631 { 632 int start, stop, incr, ndx; 633 uint_t arraycnt = (uint_t)(arraysz / sizeof (Addr)); 634 635 if (array == NULL) 636 return; 637 638 /* 639 * initarray & preinitarray are walked from beginning to end - while 640 * finiarray is walked from end to beginning. 641 */ 642 if (shtype == SHT_FINI_ARRAY) { 643 start = arraycnt - 1; 644 stop = incr = -1; 645 } else { 646 start = 0; 647 stop = arraycnt; 648 incr = 1; 649 } 650 651 /* 652 * Call the .*array[] entries 653 */ 654 for (ndx = start; ndx != stop; ndx += incr) { 655 void (*fptr)(void) = (void(*)())array[ndx]; 656 657 DBG_CALL(Dbg_util_call_array(lmp, (void *)fptr, ndx, shtype)); 658 659 leave(LIST(lmp)); 660 (*fptr)(); 661 (void) enter(); 662 } 663 } 664 665 666 /* 667 * Execute any .init sections. These are passed to us in an lmp array which 668 * (by default) will have been sorted. 669 */ 670 void 671 call_init(Rt_map **tobj, int flag) 672 { 673 Rt_map **_tobj, **_nobj; 674 static List pending = { NULL, NULL }; 675 676 /* 677 * If we're in the middle of an INITFIRST, this must complete before 678 * any new init's are fired. In this case add the object list to the 679 * pending queue and return. We'll pick up the queue after any 680 * INITFIRST objects have their init's fired. 681 */ 682 if (rtld_flags & RT_FL_INITFIRST) { 683 (void) list_append(&pending, tobj); 684 return; 685 } 686 687 /* 688 * Traverse the tobj array firing each objects init. 689 */ 690 for (_tobj = _nobj = tobj, _nobj++; *_tobj != NULL; _tobj++, _nobj++) { 691 Rt_map *lmp = *_tobj; 692 void (*iptr)() = INIT(lmp); 693 694 if (FLAGS(lmp) & FLG_RT_INITCALL) 695 continue; 696 697 FLAGS(lmp) |= FLG_RT_INITCALL; 698 699 /* 700 * Establish an initfirst state if necessary - no other inits 701 * will be fired (because of additional relocation bindings) 702 * when in this state. 703 */ 704 if (FLAGS(lmp) & FLG_RT_INITFRST) 705 rtld_flags |= RT_FL_INITFIRST; 706 707 if (INITARRAY(lmp) || iptr) { 708 Aliste idx; 709 Bnd_desc *bdp; 710 711 /* 712 * Make sure that all dependencies that have been 713 * relocated to are initialized before this objects 714 * .init is executed. This insures that a dependency 715 * on an external item that must first be initialized 716 * by its associated object is satisfied. 717 */ 718 for (APLIST_TRAVERSE(DEPENDS(lmp), idx, bdp)) { 719 if ((bdp->b_flags & BND_REFER) == 0) 720 continue; 721 is_dep_ready(bdp->b_depend, lmp, DBG_WAIT_INIT); 722 } 723 DBG_CALL(Dbg_util_call_init(lmp, flag)); 724 } 725 726 if (iptr) { 727 leave(LIST(lmp)); 728 (*iptr)(); 729 (void) enter(); 730 } 731 732 call_array(INITARRAY(lmp), INITARRAYSZ(lmp), lmp, 733 SHT_INIT_ARRAY); 734 735 if (INITARRAY(lmp) || iptr) 736 DBG_CALL(Dbg_util_call_init(lmp, DBG_INIT_DONE)); 737 738 /* 739 * Set the initdone flag regardless of whether this object 740 * actually contains an .init section. This flag prevents us 741 * from processing this section again for an .init and also 742 * signifies that a .fini must be called should it exist. 743 * Clear the sort field for use in later .fini processing. 744 */ 745 FLAGS(lmp) |= FLG_RT_INITDONE; 746 SORTVAL(lmp) = -1; 747 748 /* 749 * Wake anyone up who might be waiting on this .init. 750 */ 751 if (FLAGS1(lmp) & FL1_RT_INITWAIT) { 752 DBG_CALL(Dbg_util_broadcast(lmp)); 753 (void) rt_cond_broadcast(CONDVAR(lmp)); 754 FLAGS1(lmp) &= ~FL1_RT_INITWAIT; 755 } 756 757 /* 758 * If we're firing an INITFIRST object, and other objects must 759 * be fired which are not INITFIRST, make sure we grab any 760 * pending objects that might have been delayed as this 761 * INITFIRST was processed. 762 */ 763 if ((rtld_flags & RT_FL_INITFIRST) && 764 ((*_nobj == NULL) || !(FLAGS(*_nobj) & FLG_RT_INITFRST))) { 765 Listnode *lnp; 766 Rt_map **pobj; 767 768 rtld_flags &= ~RT_FL_INITFIRST; 769 770 while ((lnp = pending.head) != NULL) { 771 if ((pending.head = lnp->next) == NULL) 772 pending.tail = NULL; 773 pobj = lnp->data; 774 free(lnp); 775 776 call_init(pobj, DBG_INIT_PEND); 777 } 778 } 779 } 780 free(tobj); 781 } 782 783 /* 784 * Function called by atexit(3C). Calls all .fini sections related with the 785 * mains dependent shared libraries in the order in which the shared libraries 786 * have been loaded. Skip any .fini defined in the main executable, as this 787 * will be called by crt0 (main was never marked as initdone). 788 */ 789 void 790 call_fini(Lm_list * lml, Rt_map ** tobj) 791 { 792 Rt_map **_tobj; 793 794 for (_tobj = tobj; *_tobj != NULL; _tobj++) { 795 Rt_map *clmp, * lmp = *_tobj; 796 Aliste idx; 797 Bnd_desc *bdp; 798 799 /* 800 * If concurrency checking isn't enabled only fire .fini if 801 * .init has completed. We collect all .fini sections of 802 * objects that had their .init collected, but that doesn't 803 * mean at the time that the .init had completed. 804 */ 805 if ((rtld_flags & RT_FL_CONCUR) || 806 (FLAGS(lmp) & FLG_RT_INITDONE)) { 807 void (*fptr)(void) = FINI(lmp); 808 809 if (FINIARRAY(lmp) || fptr) { 810 /* 811 * If concurrency checking is enabled make sure 812 * this object's .init is completed before 813 * calling any .fini. 814 */ 815 is_dep_ready(lmp, lmp, DBG_WAIT_FINI); 816 DBG_CALL(Dbg_util_call_fini(lmp)); 817 } 818 819 call_array(FINIARRAY(lmp), FINIARRAYSZ(lmp), lmp, 820 SHT_FINI_ARRAY); 821 822 if (fptr) { 823 leave(LIST(lmp)); 824 (*fptr)(); 825 (void) enter(); 826 } 827 } 828 829 /* 830 * Skip main, this is explicitly called last in atexit_fini(). 831 */ 832 if (FLAGS(lmp) & FLG_RT_ISMAIN) 833 continue; 834 835 /* 836 * Audit `close' operations at this point. The library has 837 * exercised its last instructions (regardless of whether it 838 * will be unmapped or not). 839 * 840 * First call any global auditing. 841 */ 842 if (lml->lm_tflags & LML_TFLG_AUD_OBJCLOSE) 843 _audit_objclose(&(auditors->ad_list), lmp); 844 845 /* 846 * Finally determine whether this object has local auditing 847 * requirements by inspecting itself and then its dependencies. 848 */ 849 if ((lml->lm_flags & LML_FLG_LOCAUDIT) == 0) 850 continue; 851 852 if (FLAGS1(lmp) & LML_TFLG_AUD_OBJCLOSE) 853 _audit_objclose(&(AUDITORS(lmp)->ad_list), lmp); 854 855 for (APLIST_TRAVERSE(CALLERS(lmp), idx, bdp)) { 856 clmp = bdp->b_caller; 857 858 if (FLAGS1(clmp) & LML_TFLG_AUD_OBJCLOSE) { 859 _audit_objclose(&(AUDITORS(clmp)->ad_list), 860 lmp); 861 break; 862 } 863 } 864 } 865 DBG_CALL(Dbg_bind_plt_summary(lml, M_MACH, pltcnt21d, pltcnt24d, 866 pltcntu32, pltcntu44, pltcntfull, pltcntfar)); 867 868 free(tobj); 869 } 870 871 void 872 atexit_fini() 873 { 874 Rt_map **tobj, *lmp; 875 Lm_list *lml; 876 Listnode *lnp; 877 878 (void) enter(); 879 880 rtld_flags |= RT_FL_ATEXIT; 881 882 lml = &lml_main; 883 lml->lm_flags |= LML_FLG_ATEXIT; 884 lml->lm_flags &= ~LML_FLG_INTRPOSETSORT; 885 lmp = (Rt_map *)lml->lm_head; 886 887 /* 888 * Reverse topologically sort the main link-map for .fini execution. 889 */ 890 if (((tobj = tsort(lmp, lml->lm_obj, RT_SORT_FWD)) != 0) && 891 (tobj != (Rt_map **)S_ERROR)) 892 call_fini(lml, tobj); 893 894 /* 895 * Add an explicit close to main and ld.so.1. Although main's .fini is 896 * collected in call_fini() to provide for FINITARRAY processing, its 897 * audit_objclose is explicitly skipped. This provides for it to be 898 * called last, here. This is the reverse of the explicit calls to 899 * audit_objopen() made in setup(). 900 */ 901 if ((lml->lm_tflags | FLAGS1(lmp)) & LML_TFLG_AUD_MASK) { 902 audit_objclose(lmp, (Rt_map *)lml_rtld.lm_head); 903 audit_objclose(lmp, lmp); 904 } 905 906 /* 907 * Now that all .fini code has been run, see what unreferenced objects 908 * remain. 909 */ 910 unused(lml); 911 912 /* 913 * Traverse any alternative link-map lists. 914 */ 915 for (LIST_TRAVERSE(&dynlm_list, lnp, lml)) { 916 /* 917 * Ignore the base-link-map list, which has already been 918 * processed, and the runtime linkers link-map list, which is 919 * typically processed last. 920 */ 921 if (lml->lm_flags & (LML_FLG_BASELM | LML_FLG_RTLDLM)) 922 continue; 923 924 if ((lmp = (Rt_map *)lml->lm_head) == 0) 925 continue; 926 927 lml->lm_flags |= LML_FLG_ATEXIT; 928 lml->lm_flags &= ~LML_FLG_INTRPOSETSORT; 929 930 /* 931 * Reverse topologically sort the link-map for .fini execution. 932 */ 933 if (((tobj = tsort(lmp, lml->lm_obj, RT_SORT_FWD)) != 0) && 934 (tobj != (Rt_map **)S_ERROR)) 935 call_fini(lml, tobj); 936 937 unused(lml); 938 } 939 940 /* 941 * Finally reverse topologically sort the runtime linkers link-map for 942 * .fini execution. 943 */ 944 lml = &lml_rtld; 945 lml->lm_flags |= LML_FLG_ATEXIT; 946 lml->lm_flags &= ~LML_FLG_INTRPOSETSORT; 947 lmp = (Rt_map *)lml->lm_head; 948 949 if (((tobj = tsort(lmp, lml->lm_obj, RT_SORT_FWD)) != 0) && 950 (tobj != (Rt_map **)S_ERROR)) 951 call_fini(lml, tobj); 952 953 leave(&lml_main); 954 } 955 956 957 /* 958 * This routine is called to complete any runtime linker activity which may have 959 * resulted in objects being loaded. This is called from all user entry points 960 * and from any internal dl*() requests. 961 */ 962 void 963 load_completion(Rt_map *nlmp) 964 { 965 Rt_map **tobj = 0; 966 Lm_list *nlml; 967 968 /* 969 * Establish any .init processing. Note, in a world of lazy loading, 970 * objects may have been loaded regardless of whether the users request 971 * was fulfilled (i.e., a dlsym() request may have failed to find a 972 * symbol but objects might have been loaded during its search). Thus, 973 * any tsorting starts from the nlmp (new link-maps) pointer and not 974 * necessarily from the link-map that may have satisfied the request. 975 * 976 * Note, the primary link-map has an initialization phase where dynamic 977 * .init firing is suppressed. This provides for a simple and clean 978 * handshake with the primary link-maps libc, which is important for 979 * establishing uberdata. In addition, auditors often obtain handles 980 * to primary link-map objects as the objects are loaded, so as to 981 * inspect the link-map for symbols. This inspection is allowed without 982 * running any code on the primary link-map, as running this code may 983 * reenter the auditor, who may not yet have finished its own 984 * initialization. 985 */ 986 if (nlmp) 987 nlml = LIST(nlmp); 988 989 if (nlmp && nlml->lm_init && ((nlml != &lml_main) || 990 (rtld_flags2 & (RT_FL2_PLMSETUP | RT_FL2_NOPLM)))) { 991 if ((tobj = tsort(nlmp, LIST(nlmp)->lm_init, 992 RT_SORT_REV)) == (Rt_map **)S_ERROR) 993 tobj = 0; 994 } 995 996 /* 997 * Make sure any alternative link-map retrieves any external interfaces 998 * and initializes threads. 999 */ 1000 if (nlmp && (nlml != &lml_main)) { 1001 (void) rt_get_extern(nlml, nlmp); 1002 rt_thr_init(nlml); 1003 } 1004 1005 /* 1006 * Traverse the list of new link-maps and register any dynamic TLS. 1007 * This storage is established for any objects not on the primary 1008 * link-map, and for any objects added to the primary link-map after 1009 * static TLS has been registered. 1010 */ 1011 if (nlmp && nlml->lm_tls && ((nlml != &lml_main) || 1012 (rtld_flags2 & (RT_FL2_PLMSETUP | RT_FL2_NOPLM)))) { 1013 Rt_map *lmp; 1014 1015 for (lmp = nlmp; lmp; lmp = (Rt_map *)NEXT(lmp)) { 1016 if (PTTLS(lmp) && PTTLS(lmp)->p_memsz) 1017 tls_modaddrem(lmp, TM_FLG_MODADD); 1018 } 1019 nlml->lm_tls = 0; 1020 } 1021 1022 /* 1023 * Fire any .init's. 1024 */ 1025 if (tobj) 1026 call_init(tobj, DBG_INIT_SORT); 1027 } 1028 1029 /* 1030 * Append an item to the specified list, and return a pointer to the list 1031 * node created. 1032 */ 1033 Listnode * 1034 list_append(List *lst, const void *item) 1035 { 1036 Listnode *_lnp; 1037 1038 if ((_lnp = malloc(sizeof (Listnode))) == 0) 1039 return (0); 1040 1041 _lnp->data = (void *)item; 1042 _lnp->next = NULL; 1043 1044 if (lst->head == NULL) 1045 lst->tail = lst->head = _lnp; 1046 else { 1047 lst->tail->next = _lnp; 1048 lst->tail = lst->tail->next; 1049 } 1050 return (_lnp); 1051 } 1052 1053 1054 /* 1055 * Add an item after specified listnode, and return a pointer to the list 1056 * node created. 1057 */ 1058 Listnode * 1059 list_insert(List *lst, const void *item, Listnode *lnp) 1060 { 1061 Listnode *_lnp; 1062 1063 if ((_lnp = malloc(sizeof (Listnode))) == (Listnode *)0) 1064 return (0); 1065 1066 _lnp->data = (void *)item; 1067 _lnp->next = lnp->next; 1068 if (_lnp->next == NULL) 1069 lst->tail = _lnp; 1070 lnp->next = _lnp; 1071 return (_lnp); 1072 } 1073 1074 /* 1075 * Prepend an item to the specified list, and return a pointer to the 1076 * list node created. 1077 */ 1078 Listnode * 1079 list_prepend(List * lst, const void * item) 1080 { 1081 Listnode *_lnp; 1082 1083 if ((_lnp = malloc(sizeof (Listnode))) == (Listnode *)0) 1084 return (0); 1085 1086 _lnp->data = (void *)item; 1087 1088 if (lst->head == NULL) { 1089 _lnp->next = NULL; 1090 lst->tail = lst->head = _lnp; 1091 } else { 1092 _lnp->next = lst->head; 1093 lst->head = _lnp; 1094 } 1095 return (_lnp); 1096 } 1097 1098 1099 /* 1100 * Delete a 'listnode' from a list. 1101 */ 1102 void 1103 list_delete(List *lst, void *item) 1104 { 1105 Listnode *clnp, *plnp; 1106 1107 for (plnp = NULL, clnp = lst->head; clnp; clnp = clnp->next) { 1108 if (item == clnp->data) 1109 break; 1110 plnp = clnp; 1111 } 1112 1113 if (clnp == 0) 1114 return; 1115 1116 if (lst->head == clnp) 1117 lst->head = clnp->next; 1118 if (lst->tail == clnp) 1119 lst->tail = plnp; 1120 1121 if (plnp) 1122 plnp->next = clnp->next; 1123 1124 free(clnp); 1125 } 1126 1127 /* 1128 * Append an item to the specified link map control list. 1129 */ 1130 void 1131 lm_append(Lm_list *lml, Aliste lmco, Rt_map *lmp) 1132 { 1133 Lm_cntl *lmc; 1134 int add = 1; 1135 1136 /* 1137 * Indicate that this link-map list has a new object. 1138 */ 1139 (lml->lm_obj)++; 1140 1141 /* 1142 * If we're about to add a new object to the main link-map control list, 1143 * alert the debuggers that we are about to mess with this list. 1144 * Additions of individual objects to the main link-map control list 1145 * occur during initial setup as the applications immediate dependencies 1146 * are loaded. Individual objects are also loaded on the main link-map 1147 * control list of new alternative link-map control lists. 1148 */ 1149 if ((lmco == ALIST_OFF_DATA) && 1150 ((lml->lm_flags & LML_FLG_DBNOTIF) == 0)) 1151 rd_event(lml, RD_DLACTIVITY, RT_ADD); 1152 1153 /* LINTED */ 1154 lmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, lmco); 1155 1156 /* 1157 * A link-map list header points to one of more link-map control lists 1158 * (see include/rtld.h). The initial list, pointed to by lm_cntl, is 1159 * the list of relocated objects. Other lists maintain objects that 1160 * are still being analyzed or relocated. This list provides the core 1161 * link-map list information used by all ld.so.1 routines. 1162 */ 1163 if (lmc->lc_head == NULL) { 1164 /* 1165 * If this is the first link-map for the given control list, 1166 * initialize the list. 1167 */ 1168 lmc->lc_head = lmc->lc_tail = lmp; 1169 add = 0; 1170 1171 } else if (FLAGS(lmp) & FLG_RT_OBJINTPO) { 1172 Rt_map *tlmp; 1173 1174 /* 1175 * If this is an interposer then append the link-map following 1176 * any other interposers (these are objects that have been 1177 * previously preloaded, or were identified with -z interpose). 1178 * Interposers can only be inserted on the first link-map 1179 * control list, as once relocation has started, interposition 1180 * from new interposers can't be guaranteed. 1181 * 1182 * NOTE: We do not interpose on the head of a list. This model 1183 * evolved because dynamic executables have already been fully 1184 * relocated within themselves and thus can't be interposed on. 1185 * Nowadays it's possible to have shared objects at the head of 1186 * a list, which conceptually means they could be interposed on. 1187 * But, shared objects can be created via dldump() and may only 1188 * be partially relocated (just relatives), in which case they 1189 * are interposable, but are marked as fixed (ET_EXEC). 1190 * 1191 * Thus we really don't have a clear method of deciding when the 1192 * head of a link-map is interposable. So, to be consistent, 1193 * for now only add interposers after the link-map lists head 1194 * object. 1195 */ 1196 for (tlmp = (Rt_map *)NEXT(lmc->lc_head); tlmp; 1197 tlmp = (Rt_map *)NEXT(tlmp)) { 1198 1199 if (FLAGS(tlmp) & FLG_RT_OBJINTPO) 1200 continue; 1201 1202 /* 1203 * Insert the new link-map before this non-interposer, 1204 * and indicate an interposer is found. 1205 */ 1206 NEXT((Rt_map *)PREV(tlmp)) = (Link_map *)lmp; 1207 PREV(lmp) = PREV(tlmp); 1208 1209 NEXT(lmp) = (Link_map *)tlmp; 1210 PREV(tlmp) = (Link_map *)lmp; 1211 1212 lmc->lc_flags |= LMC_FLG_REANALYZE; 1213 add = 0; 1214 break; 1215 } 1216 } 1217 1218 /* 1219 * Fall through to appending the new link map to the tail of the list. 1220 * If we're processing the initial objects of this link-map list, add 1221 * them to the backward compatibility list. 1222 */ 1223 if (add) { 1224 NEXT(lmc->lc_tail) = (Link_map *)lmp; 1225 PREV(lmp) = (Link_map *)lmc->lc_tail; 1226 lmc->lc_tail = lmp; 1227 } 1228 1229 /* 1230 * Having added this link-map to a control list, indicate which control 1231 * list the link-map belongs to. Note, control list information is 1232 * always maintained as an offset, as the Alist can be reallocated. 1233 */ 1234 CNTL(lmp) = lmco; 1235 1236 /* 1237 * Indicate if an interposer is found. Note that the first object on a 1238 * link-map can be explicitly defined as an interposer so that it can 1239 * provide interposition over direct binding requests. 1240 */ 1241 if (FLAGS(lmp) & MSK_RT_INTPOSE) 1242 lml->lm_flags |= LML_FLG_INTRPOSE; 1243 1244 /* 1245 * For backward compatibility with debuggers, the link-map list contains 1246 * pointers to the main control list. 1247 */ 1248 if (lmco == ALIST_OFF_DATA) { 1249 lml->lm_head = lmc->lc_head; 1250 lml->lm_tail = lmc->lc_tail; 1251 } 1252 } 1253 1254 /* 1255 * Delete an item from the specified link map control list. 1256 */ 1257 void 1258 lm_delete(Lm_list *lml, Rt_map *lmp) 1259 { 1260 Lm_cntl *lmc; 1261 1262 /* 1263 * If the control list pointer hasn't been initialized, this object 1264 * never got added to a link-map list. 1265 */ 1266 if (CNTL(lmp) == 0) 1267 return; 1268 1269 /* 1270 * If we're about to delete an object from the main link-map control 1271 * list, alert the debuggers that we are about to mess with this list. 1272 */ 1273 if ((CNTL(lmp) == ALIST_OFF_DATA) && 1274 ((lml->lm_flags & LML_FLG_DBNOTIF) == 0)) 1275 rd_event(lml, RD_DLACTIVITY, RT_DELETE); 1276 1277 /* LINTED */ 1278 lmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, CNTL(lmp)); 1279 1280 if (lmc->lc_head == lmp) 1281 lmc->lc_head = (Rt_map *)NEXT(lmp); 1282 else 1283 NEXT((Rt_map *)PREV(lmp)) = (void *)NEXT(lmp); 1284 1285 if (lmc->lc_tail == lmp) 1286 lmc->lc_tail = (Rt_map *)PREV(lmp); 1287 else 1288 PREV((Rt_map *)NEXT(lmp)) = PREV(lmp); 1289 1290 /* 1291 * For backward compatibility with debuggers, the link-map list contains 1292 * pointers to the main control list. 1293 */ 1294 if (lmc == (Lm_cntl *)&lml->lm_lists->al_data) { 1295 lml->lm_head = lmc->lc_head; 1296 lml->lm_tail = lmc->lc_tail; 1297 } 1298 1299 /* 1300 * Indicate we have one less object on this control list. 1301 */ 1302 (lml->lm_obj)--; 1303 } 1304 1305 /* 1306 * Move a link-map control list to another. Objects that are being relocated 1307 * are maintained on secondary control lists. Once their relocation is 1308 * complete, the entire list is appended to the previous control list, as this 1309 * list must have been the trigger for generating the new control list. 1310 */ 1311 void 1312 lm_move(Lm_list *lml, Aliste nlmco, Aliste plmco, Lm_cntl *nlmc, Lm_cntl *plmc) 1313 { 1314 Rt_map *lmp; 1315 1316 /* 1317 * If we're about to add a new family of objects to the main link-map 1318 * control list, alert the debuggers that we are about to mess with this 1319 * list. Additions of object families to the main link-map control 1320 * list occur during lazy loading, filtering and dlopen(). 1321 */ 1322 if ((plmco == ALIST_OFF_DATA) && 1323 ((lml->lm_flags & LML_FLG_DBNOTIF) == 0)) 1324 rd_event(lml, RD_DLACTIVITY, RT_ADD); 1325 1326 DBG_CALL(Dbg_file_cntl(lml, nlmco, plmco)); 1327 1328 /* 1329 * Indicate each new link-map has been moved to the previous link-map 1330 * control list. 1331 */ 1332 for (lmp = nlmc->lc_head; lmp; lmp = (Rt_map *)NEXT(lmp)) 1333 CNTL(lmp) = plmco; 1334 1335 /* 1336 * Move the new link-map control list, to the callers link-map control 1337 * list. 1338 */ 1339 if (plmc->lc_head == 0) { 1340 plmc->lc_head = nlmc->lc_head; 1341 PREV(nlmc->lc_head) = 0; 1342 } else { 1343 NEXT(plmc->lc_tail) = (Link_map *)nlmc->lc_head; 1344 PREV(nlmc->lc_head) = (Link_map *)plmc->lc_tail; 1345 } 1346 1347 plmc->lc_tail = nlmc->lc_tail; 1348 nlmc->lc_head = nlmc->lc_tail = 0; 1349 1350 /* 1351 * For backward compatibility with debuggers, the link-map list contains 1352 * pointers to the main control list. 1353 */ 1354 if (plmco == ALIST_OFF_DATA) { 1355 lml->lm_head = plmc->lc_head; 1356 lml->lm_tail = plmc->lc_tail; 1357 } 1358 } 1359 1360 /* 1361 * Environment variables can have a variety of defined permutations, and thus 1362 * the following infrastructure exists to allow this variety and to select the 1363 * required definition. 1364 * 1365 * Environment variables can be defined as 32- or 64-bit specific, and if so 1366 * they will take precedence over any instruction set neutral form. Typically 1367 * this is only useful when the environment value is an informational string. 1368 * 1369 * Environment variables may be obtained from the standard user environment or 1370 * from a configuration file. The latter provides a fallback if no user 1371 * environment setting is found, and can take two forms: 1372 * 1373 * . a replaceable definition - this will be used if no user environment 1374 * setting has been seen, or 1375 * 1376 * . an permanent definition - this will be used no matter what user 1377 * environment setting is seen. In the case of list variables it will be 1378 * appended to any process environment setting seen. 1379 * 1380 * Environment variables can be defined without a value (ie. LD_XXXX=) so as to 1381 * override any replaceable environment variables from a configuration file. 1382 */ 1383 static u_longlong_t rplgen; /* replaceable generic */ 1384 /* variables */ 1385 static u_longlong_t rplisa; /* replaceable ISA specific */ 1386 /* variables */ 1387 static u_longlong_t prmgen; /* permanent generic */ 1388 /* variables */ 1389 static u_longlong_t prmisa; /* permanent ISA specific */ 1390 /* variables */ 1391 1392 /* 1393 * Classify an environment variables type. 1394 */ 1395 #define ENV_TYP_IGNORE 0x1 /* ignore - variable is for */ 1396 /* the wrong ISA */ 1397 #define ENV_TYP_ISA 0x2 /* variable is ISA specific */ 1398 #define ENV_TYP_CONFIG 0x4 /* variable obtained from a */ 1399 /* config file */ 1400 #define ENV_TYP_PERMANT 0x8 /* variable is permanent */ 1401 1402 /* 1403 * Identify all environment variables. 1404 */ 1405 #define ENV_FLG_AUDIT 0x0000000001ULL 1406 #define ENV_FLG_AUDIT_ARGS 0x0000000002ULL 1407 #define ENV_FLG_BIND_NOW 0x0000000004ULL 1408 #define ENV_FLG_BIND_NOT 0x0000000008ULL 1409 #define ENV_FLG_BINDINGS 0x0000000010ULL 1410 #define ENV_FLG_CONCURRENCY 0x0000000020ULL 1411 #define ENV_FLG_CONFGEN 0x0000000040ULL 1412 #define ENV_FLG_CONFIG 0x0000000080ULL 1413 #define ENV_FLG_DEBUG 0x0000000100ULL 1414 #define ENV_FLG_DEBUG_OUTPUT 0x0000000200ULL 1415 #define ENV_FLG_DEMANGLE 0x0000000400ULL 1416 #define ENV_FLG_FLAGS 0x0000000800ULL 1417 #define ENV_FLG_INIT 0x0000001000ULL 1418 #define ENV_FLG_LIBPATH 0x0000002000ULL 1419 #define ENV_FLG_LOADAVAIL 0x0000004000ULL 1420 #define ENV_FLG_LOADFLTR 0x0000008000ULL 1421 #define ENV_FLG_NOAUDIT 0x0000010000ULL 1422 #define ENV_FLG_NOAUXFLTR 0x0000020000ULL 1423 #define ENV_FLG_NOBAPLT 0x0000040000ULL 1424 #define ENV_FLG_NOCONFIG 0x0000080000ULL 1425 #define ENV_FLG_NODIRCONFIG 0x0000100000ULL 1426 #define ENV_FLG_NODIRECT 0x0000200000ULL 1427 #define ENV_FLG_NOENVCONFIG 0x0000400000ULL 1428 #define ENV_FLG_NOLAZY 0x0000800000ULL 1429 #define ENV_FLG_NOOBJALTER 0x0001000000ULL 1430 #define ENV_FLG_NOVERSION 0x0002000000ULL 1431 #define ENV_FLG_PRELOAD 0x0004000000ULL 1432 #define ENV_FLG_PROFILE 0x0008000000ULL 1433 #define ENV_FLG_PROFILE_OUTPUT 0x0010000000ULL 1434 #define ENV_FLG_SIGNAL 0x0020000000ULL 1435 #define ENV_FLG_TRACE_OBJS 0x0040000000ULL 1436 #define ENV_FLG_TRACE_PTHS 0x0080000000ULL 1437 #define ENV_FLG_UNREF 0x0100000000ULL 1438 #define ENV_FLG_UNUSED 0x0200000000ULL 1439 #define ENV_FLG_VERBOSE 0x0400000000ULL 1440 #define ENV_FLG_WARN 0x0800000000ULL 1441 #define ENV_FLG_NOFLTCONFIG 0x1000000000ULL 1442 #define ENV_FLG_BIND_LAZY 0x2000000000ULL 1443 #define ENV_FLG_NOUNRESWEAK 0x4000000000ULL 1444 #define ENV_FLG_NOPAREXT 0x8000000000ULL 1445 1446 #ifdef SIEBEL_DISABLE 1447 #define ENV_FLG_FIX_1 0x8000000000ULL 1448 #endif 1449 1450 #define SEL_REPLACE 0x0001 1451 #define SEL_PERMANT 0x0002 1452 #define SEL_ACT_RT 0x0100 /* setting rtld_flags */ 1453 #define SEL_ACT_RT2 0x0200 /* setting rtld_flags2 */ 1454 #define SEL_ACT_STR 0x0400 /* setting string value */ 1455 #define SEL_ACT_LML 0x0800 /* setting lml_flags */ 1456 #define SEL_ACT_LMLT 0x1000 /* setting lml_tflags */ 1457 #define SEL_ACT_SPEC_1 0x2000 /* For FLG_{FLAGS, LIBPATH} */ 1458 #define SEL_ACT_SPEC_2 0x4000 /* need special handling */ 1459 1460 /* 1461 * Pattern match an LD_XXXX environment variable. s1 points to the XXXX part 1462 * and len specifies its length (comparing a strings length before the string 1463 * itself speed things up). s2 points to the token itself which has already 1464 * had any leading white-space removed. 1465 */ 1466 static void 1467 ld_generic_env(const char *s1, size_t len, const char *s2, Word *lmflags, 1468 Word *lmtflags, uint_t env_flags, int aout) 1469 { 1470 u_longlong_t variable = 0; 1471 ushort_t select = 0; 1472 const char **str; 1473 Word val = 0; 1474 1475 /* 1476 * Determine whether we're dealing with a replaceable or permanent 1477 * string. 1478 */ 1479 if (env_flags & ENV_TYP_PERMANT) { 1480 /* 1481 * If the string is from a configuration file and defined as 1482 * permanent, assign it as permanent. 1483 */ 1484 select |= SEL_PERMANT; 1485 } else 1486 select |= SEL_REPLACE; 1487 1488 /* 1489 * Parse the variable given. 1490 * 1491 * The LD_AUDIT family. 1492 */ 1493 if (*s1 == 'A') { 1494 if ((len == MSG_LD_AUDIT_SIZE) && (strncmp(s1, 1495 MSG_ORIG(MSG_LD_AUDIT), MSG_LD_AUDIT_SIZE) == 0)) { 1496 /* 1497 * Replaceable and permanent audit objects can exist. 1498 */ 1499 select |= SEL_ACT_STR; 1500 if (select & SEL_REPLACE) 1501 str = &rpl_audit; 1502 else { 1503 str = &prm_audit; 1504 rpl_audit = 0; 1505 } 1506 variable = ENV_FLG_AUDIT; 1507 } else if ((len == MSG_LD_AUDIT_ARGS_SIZE) && 1508 (strncmp(s1, MSG_ORIG(MSG_LD_AUDIT_ARGS), 1509 MSG_LD_AUDIT_ARGS_SIZE) == 0)) { 1510 /* 1511 * A specialized variable for plt_exit() use, not 1512 * documented for general use. 1513 */ 1514 select |= SEL_ACT_SPEC_2; 1515 variable = ENV_FLG_AUDIT_ARGS; 1516 } 1517 } 1518 /* 1519 * The LD_BIND family and LD_BREADTH (historic). 1520 */ 1521 else if (*s1 == 'B') { 1522 if ((len == MSG_LD_BIND_LAZY_SIZE) && (strncmp(s1, 1523 MSG_ORIG(MSG_LD_BIND_LAZY), 1524 MSG_LD_BIND_LAZY_SIZE) == 0)) { 1525 select |= SEL_ACT_RT2; 1526 val = RT_FL2_BINDLAZY; 1527 variable = ENV_FLG_BIND_LAZY; 1528 } else if ((len == MSG_LD_BIND_NOW_SIZE) && (strncmp(s1, 1529 MSG_ORIG(MSG_LD_BIND_NOW), MSG_LD_BIND_NOW_SIZE) == 0)) { 1530 select |= SEL_ACT_RT2; 1531 val = RT_FL2_BINDNOW; 1532 variable = ENV_FLG_BIND_NOW; 1533 } else if ((len == MSG_LD_BIND_NOT_SIZE) && (strncmp(s1, 1534 MSG_ORIG(MSG_LD_BIND_NOT), MSG_LD_BIND_NOT_SIZE) == 0)) { 1535 /* 1536 * Another trick, enabled to help debug AOUT 1537 * applications under BCP, but not documented for 1538 * general use. 1539 */ 1540 select |= SEL_ACT_RT; 1541 val = RT_FL_NOBIND; 1542 variable = ENV_FLG_BIND_NOT; 1543 } else if ((len == MSG_LD_BINDINGS_SIZE) && (strncmp(s1, 1544 MSG_ORIG(MSG_LD_BINDINGS), MSG_LD_BINDINGS_SIZE) == 0)) { 1545 /* 1546 * This variable is simply for backward compatibility. 1547 * If this and LD_DEBUG are both specified, only one of 1548 * the strings is going to get processed. 1549 */ 1550 select |= SEL_ACT_SPEC_2; 1551 variable = ENV_FLG_BINDINGS; 1552 #ifndef LD_BREADTH_DISABLED 1553 } else if ((len == MSG_LD_BREADTH_SIZE) && (strncmp(s1, 1554 MSG_ORIG(MSG_LD_BREADTH), MSG_LD_BREADTH_SIZE) == 0)) { 1555 /* 1556 * Besides some old patches this is no longer available. 1557 */ 1558 rtld_flags |= RT_FL_BREADTH; 1559 return; 1560 #endif 1561 } 1562 } 1563 /* 1564 * LD_CONCURRENCY and LD_CONFIG family. 1565 */ 1566 else if (*s1 == 'C') { 1567 if ((len == MSG_LD_CONCURRENCY_SIZE) && (strncmp(s1, 1568 MSG_ORIG(MSG_LD_CONCURRENCY), 1569 MSG_LD_CONCURRENCY_SIZE) == 0)) { 1570 /* 1571 * Waiting in the wings, as concurrency checking isn't 1572 * yet enabled. 1573 */ 1574 select |= SEL_ACT_SPEC_2; 1575 variable = ENV_FLG_CONCURRENCY; 1576 } else if ((len == MSG_LD_CONFGEN_SIZE) && (strncmp(s1, 1577 MSG_ORIG(MSG_LD_CONFGEN), MSG_LD_CONFGEN_SIZE) == 0)) { 1578 /* 1579 * Set by crle(1) to indicate it's building a 1580 * configuration file, not documented for general use. 1581 */ 1582 select |= SEL_ACT_SPEC_2; 1583 variable = ENV_FLG_CONFGEN; 1584 } else if ((len == MSG_LD_CONFIG_SIZE) && (strncmp(s1, 1585 MSG_ORIG(MSG_LD_CONFIG), MSG_LD_CONFIG_SIZE) == 0)) { 1586 /* 1587 * Secure applications must use a default configuration 1588 * file. A setting from a configuration file doesn't 1589 * make sense (given we must be reading a configuration 1590 * file to have gotten this). 1591 */ 1592 if ((rtld_flags & RT_FL_SECURE) || 1593 (env_flags & ENV_TYP_CONFIG)) 1594 return; 1595 select |= SEL_ACT_STR; 1596 str = &config->c_name; 1597 variable = ENV_FLG_CONFIG; 1598 } 1599 } 1600 /* 1601 * The LD_DEBUG family and LD_DEMANGLE. 1602 */ 1603 else if (*s1 == 'D') { 1604 if ((len == MSG_LD_DEBUG_SIZE) && (strncmp(s1, 1605 MSG_ORIG(MSG_LD_DEBUG), MSG_LD_DEBUG_SIZE) == 0)) { 1606 select |= SEL_ACT_STR; 1607 if (select & SEL_REPLACE) 1608 str = &rpl_debug; 1609 else { 1610 str = &prm_debug; 1611 rpl_debug = 0; 1612 } 1613 variable = ENV_FLG_DEBUG; 1614 } else if ((len == MSG_LD_DEBUG_OUTPUT_SIZE) && (strncmp(s1, 1615 MSG_ORIG(MSG_LD_DEBUG_OUTPUT), 1616 MSG_LD_DEBUG_OUTPUT_SIZE) == 0)) { 1617 select |= SEL_ACT_STR; 1618 str = &dbg_file; 1619 variable = ENV_FLG_DEBUG_OUTPUT; 1620 } else if ((len == MSG_LD_DEMANGLE_SIZE) && (strncmp(s1, 1621 MSG_ORIG(MSG_LD_DEMANGLE), MSG_LD_DEMANGLE_SIZE) == 0)) { 1622 select |= SEL_ACT_RT; 1623 val = RT_FL_DEMANGLE; 1624 variable = ENV_FLG_DEMANGLE; 1625 } 1626 } 1627 /* 1628 * LD_FLAGS - collect the best variable definition. On completion of 1629 * environment variable processing pass the result to ld_flags_env() 1630 * where they'll be decomposed and passed back to this routine. 1631 */ 1632 else if (*s1 == 'F') { 1633 if ((len == MSG_LD_FLAGS_SIZE) && (strncmp(s1, 1634 MSG_ORIG(MSG_LD_FLAGS), MSG_LD_FLAGS_SIZE) == 0)) { 1635 select |= SEL_ACT_SPEC_1; 1636 if (select & SEL_REPLACE) 1637 str = &rpl_ldflags; 1638 else { 1639 str = &prm_ldflags; 1640 rpl_ldflags = 0; 1641 } 1642 variable = ENV_FLG_FLAGS; 1643 } 1644 } 1645 /* 1646 * LD_INIT (internal, used by ldd(1)). 1647 */ 1648 else if (*s1 == 'I') { 1649 if ((len == MSG_LD_INIT_SIZE) && (strncmp(s1, 1650 MSG_ORIG(MSG_LD_INIT), MSG_LD_INIT_SIZE) == 0)) { 1651 select |= SEL_ACT_LML; 1652 val = LML_FLG_TRC_INIT; 1653 variable = ENV_FLG_INIT; 1654 } 1655 } 1656 /* 1657 * The LD_LIBRARY_PATH and LD_LOAD families. 1658 */ 1659 else if (*s1 == 'L') { 1660 if ((len == MSG_LD_LIBPATH_SIZE) && (strncmp(s1, 1661 MSG_ORIG(MSG_LD_LIBPATH), MSG_LD_LIBPATH_SIZE) == 0)) { 1662 select |= SEL_ACT_SPEC_1; 1663 if (select & SEL_REPLACE) 1664 str = &rpl_libpath; 1665 else { 1666 str = &prm_libpath; 1667 rpl_libpath = 0; 1668 } 1669 variable = ENV_FLG_LIBPATH; 1670 } else if ((len == MSG_LD_LOADAVAIL_SIZE) && (strncmp(s1, 1671 MSG_ORIG(MSG_LD_LOADAVAIL), MSG_LD_LOADAVAIL_SIZE) == 0)) { 1672 /* 1673 * Internal use by crle(1), not documented for general 1674 * use. 1675 */ 1676 select |= SEL_ACT_LML; 1677 val = LML_FLG_LOADAVAIL; 1678 variable = ENV_FLG_LOADAVAIL; 1679 } else if ((len == MSG_LD_LOADFLTR_SIZE) && (strncmp(s1, 1680 MSG_ORIG(MSG_LD_LOADFLTR), MSG_LD_LOADFLTR_SIZE) == 0)) { 1681 select |= SEL_ACT_SPEC_2; 1682 variable = ENV_FLG_LOADFLTR; 1683 } 1684 } 1685 /* 1686 * The LD_NO family. 1687 */ 1688 else if (*s1 == 'N') { 1689 if ((len == MSG_LD_NOAUDIT_SIZE) && (strncmp(s1, 1690 MSG_ORIG(MSG_LD_NOAUDIT), MSG_LD_NOAUDIT_SIZE) == 0)) { 1691 select |= SEL_ACT_RT; 1692 val = RT_FL_NOAUDIT; 1693 variable = ENV_FLG_NOAUDIT; 1694 } else if ((len == MSG_LD_NOAUXFLTR_SIZE) && (strncmp(s1, 1695 MSG_ORIG(MSG_LD_NOAUXFLTR), MSG_LD_NOAUXFLTR_SIZE) == 0)) { 1696 select |= SEL_ACT_RT; 1697 val = RT_FL_NOAUXFLTR; 1698 variable = ENV_FLG_NOAUXFLTR; 1699 } else if ((len == MSG_LD_NOBAPLT_SIZE) && (strncmp(s1, 1700 MSG_ORIG(MSG_LD_NOBAPLT), MSG_LD_NOBAPLT_SIZE) == 0)) { 1701 select |= SEL_ACT_RT; 1702 val = RT_FL_NOBAPLT; 1703 variable = ENV_FLG_NOBAPLT; 1704 } else if ((len == MSG_LD_NOCONFIG_SIZE) && (strncmp(s1, 1705 MSG_ORIG(MSG_LD_NOCONFIG), MSG_LD_NOCONFIG_SIZE) == 0)) { 1706 select |= SEL_ACT_RT; 1707 val = RT_FL_NOCFG; 1708 variable = ENV_FLG_NOCONFIG; 1709 } else if ((len == MSG_LD_NODIRCONFIG_SIZE) && (strncmp(s1, 1710 MSG_ORIG(MSG_LD_NODIRCONFIG), 1711 MSG_LD_NODIRCONFIG_SIZE) == 0)) { 1712 select |= SEL_ACT_RT; 1713 val = RT_FL_NODIRCFG; 1714 variable = ENV_FLG_NODIRCONFIG; 1715 } else if ((len == MSG_LD_NODIRECT_SIZE) && (strncmp(s1, 1716 MSG_ORIG(MSG_LD_NODIRECT), MSG_LD_NODIRECT_SIZE) == 0)) { 1717 select |= SEL_ACT_LMLT; 1718 val = LML_TFLG_NODIRECT; 1719 variable = ENV_FLG_NODIRECT; 1720 } else if ((len == MSG_LD_NOENVCONFIG_SIZE) && (strncmp(s1, 1721 MSG_ORIG(MSG_LD_NOENVCONFIG), 1722 MSG_LD_NOENVCONFIG_SIZE) == 0)) { 1723 select |= SEL_ACT_RT; 1724 val = RT_FL_NOENVCFG; 1725 variable = ENV_FLG_NOENVCONFIG; 1726 } else if ((len == MSG_LD_NOFLTCONFIG_SIZE) && (strncmp(s1, 1727 MSG_ORIG(MSG_LD_NOFLTCONFIG), 1728 MSG_LD_NOFLTCONFIG_SIZE) == 0)) { 1729 select |= SEL_ACT_RT2; 1730 val = RT_FL2_NOFLTCFG; 1731 variable = ENV_FLG_NOFLTCONFIG; 1732 } else if ((len == MSG_LD_NOLAZY_SIZE) && (strncmp(s1, 1733 MSG_ORIG(MSG_LD_NOLAZY), MSG_LD_NOLAZY_SIZE) == 0)) { 1734 select |= SEL_ACT_LMLT; 1735 val = LML_TFLG_NOLAZYLD; 1736 variable = ENV_FLG_NOLAZY; 1737 } else if ((len == MSG_LD_NOOBJALTER_SIZE) && (strncmp(s1, 1738 MSG_ORIG(MSG_LD_NOOBJALTER), 1739 MSG_LD_NOOBJALTER_SIZE) == 0)) { 1740 select |= SEL_ACT_RT; 1741 val = RT_FL_NOOBJALT; 1742 variable = ENV_FLG_NOOBJALTER; 1743 } else if ((len == MSG_LD_NOVERSION_SIZE) && (strncmp(s1, 1744 MSG_ORIG(MSG_LD_NOVERSION), MSG_LD_NOVERSION_SIZE) == 0)) { 1745 select |= SEL_ACT_RT; 1746 val = RT_FL_NOVERSION; 1747 variable = ENV_FLG_NOVERSION; 1748 } else if ((len == MSG_LD_NOUNRESWEAK_SIZE) && (strncmp(s1, 1749 MSG_ORIG(MSG_LD_NOUNRESWEAK), 1750 MSG_LD_NOUNRESWEAK_SIZE) == 0)) { 1751 /* 1752 * LD_NOUNRESWEAK (internal, used by ldd(1)). 1753 */ 1754 select |= SEL_ACT_LML; 1755 val = LML_FLG_TRC_NOUNRESWEAK; 1756 variable = ENV_FLG_NOUNRESWEAK; 1757 } else if ((len == MSG_LD_NOPAREXT_SIZE) && (strncmp(s1, 1758 MSG_ORIG(MSG_LD_NOPAREXT), MSG_LD_NOPAREXT_SIZE) == 0)) { 1759 select |= SEL_ACT_LML; 1760 val = LML_FLG_TRC_NOPAREXT; 1761 variable = ENV_FLG_NOPAREXT; 1762 } 1763 } 1764 /* 1765 * LD_ORIGIN. 1766 */ 1767 else if (*s1 == 'O') { 1768 #ifndef EXPAND_RELATIVE 1769 if ((len == MSG_LD_ORIGIN_SIZE) && (strncmp(s1, 1770 MSG_ORIG(MSG_LD_ORIGIN), MSG_LD_ORIGIN_SIZE) == 0)) { 1771 /* 1772 * Besides some old patches this is no longer required. 1773 */ 1774 rtld_flags |= RT_FL_RELATIVE; 1775 } 1776 #endif 1777 return; 1778 } 1779 /* 1780 * LD_PRELOAD and LD_PROFILE family. 1781 */ 1782 else if (*s1 == 'P') { 1783 if ((len == MSG_LD_PRELOAD_SIZE) && (strncmp(s1, 1784 MSG_ORIG(MSG_LD_PRELOAD), MSG_LD_PRELOAD_SIZE) == 0)) { 1785 select |= SEL_ACT_STR; 1786 if (select & SEL_REPLACE) 1787 str = &rpl_preload; 1788 else { 1789 str = &prm_preload; 1790 rpl_preload = 0; 1791 } 1792 variable = ENV_FLG_PRELOAD; 1793 } else if ((len == MSG_LD_PROFILE_SIZE) && (strncmp(s1, 1794 MSG_ORIG(MSG_LD_PROFILE), MSG_LD_PROFILE_SIZE) == 0)) { 1795 /* 1796 * Only one user library can be profiled at a time. 1797 */ 1798 select |= SEL_ACT_SPEC_2; 1799 variable = ENV_FLG_PROFILE; 1800 } else if ((len == MSG_LD_PROFILE_OUTPUT_SIZE) && (strncmp(s1, 1801 MSG_ORIG(MSG_LD_PROFILE_OUTPUT), 1802 MSG_LD_PROFILE_OUTPUT_SIZE) == 0)) { 1803 /* 1804 * Only one user library can be profiled at a time. 1805 */ 1806 select |= SEL_ACT_STR; 1807 str = &profile_out; 1808 variable = ENV_FLG_PROFILE_OUTPUT; 1809 } 1810 } 1811 /* 1812 * LD_SIGNAL. 1813 */ 1814 else if (*s1 == 'S') { 1815 if (rtld_flags & RT_FL_SECURE) 1816 return; 1817 if ((len == MSG_LD_SIGNAL_SIZE) && 1818 (strncmp(s1, MSG_ORIG(MSG_LD_SIGNAL), 1819 MSG_LD_SIGNAL_SIZE) == 0)) { 1820 select |= SEL_ACT_SPEC_2; 1821 variable = ENV_FLG_SIGNAL; 1822 } 1823 } 1824 /* 1825 * The LD_TRACE family (internal, used by ldd(1)). This definition is 1826 * the key to enabling all other ldd(1) specific environment variables. 1827 * In case an auditor is called, which in turn might exec(2) a 1828 * subprocess, this variable is disabled, so that any subprocess 1829 * escapes ldd(1) processing. 1830 */ 1831 else if (*s1 == 'T') { 1832 if (((len == MSG_LD_TRACE_OBJS_SIZE) && 1833 (strncmp(s1, MSG_ORIG(MSG_LD_TRACE_OBJS), 1834 MSG_LD_TRACE_OBJS_SIZE) == 0)) || 1835 ((len == MSG_LD_TRACE_OBJS_E_SIZE) && 1836 (((strncmp(s1, MSG_ORIG(MSG_LD_TRACE_OBJS_E), 1837 MSG_LD_TRACE_OBJS_E_SIZE) == 0) && !aout) || 1838 ((strncmp(s1, MSG_ORIG(MSG_LD_TRACE_OBJS_A), 1839 MSG_LD_TRACE_OBJS_A_SIZE) == 0) && aout)))) { 1840 char *s0 = (char *)s1; 1841 1842 select |= SEL_ACT_SPEC_2; 1843 variable = ENV_FLG_TRACE_OBJS; 1844 1845 #if defined(__sparc) || defined(__x86) 1846 /* 1847 * The simplest way to "disable" this variable is to 1848 * truncate this string to "LD_'\0'". This string is 1849 * ignored by any ld.so.1 environment processing. 1850 * Use of such interfaces as unsetenv(3c) are overkill, 1851 * and would drag too much libc implementation detail 1852 * into ld.so.1. 1853 */ 1854 *s0 = '\0'; 1855 #else 1856 /* 1857 * Verify that the above write is appropriate for any new platforms. 1858 */ 1859 #error unsupported architecture! 1860 #endif 1861 } else if ((len == MSG_LD_TRACE_PTHS_SIZE) && (strncmp(s1, 1862 MSG_ORIG(MSG_LD_TRACE_PTHS), 1863 MSG_LD_TRACE_PTHS_SIZE) == 0)) { 1864 select |= SEL_ACT_LML; 1865 val = LML_FLG_TRC_SEARCH; 1866 variable = ENV_FLG_TRACE_PTHS; 1867 } 1868 } 1869 /* 1870 * LD_UNREF and LD_UNUSED (internal, used by ldd(1)). 1871 */ 1872 else if (*s1 == 'U') { 1873 if ((len == MSG_LD_UNREF_SIZE) && (strncmp(s1, 1874 MSG_ORIG(MSG_LD_UNREF), MSG_LD_UNREF_SIZE) == 0)) { 1875 select |= SEL_ACT_LML; 1876 val = LML_FLG_TRC_UNREF; 1877 variable = ENV_FLG_UNREF; 1878 } else if ((len == MSG_LD_UNUSED_SIZE) && (strncmp(s1, 1879 MSG_ORIG(MSG_LD_UNUSED), MSG_LD_UNUSED_SIZE) == 0)) { 1880 select |= SEL_ACT_LML; 1881 val = LML_FLG_TRC_UNUSED; 1882 variable = ENV_FLG_UNUSED; 1883 } 1884 } 1885 /* 1886 * LD_VERBOSE (internal, used by ldd(1)). 1887 */ 1888 else if (*s1 == 'V') { 1889 if ((len == MSG_LD_VERBOSE_SIZE) && (strncmp(s1, 1890 MSG_ORIG(MSG_LD_VERBOSE), MSG_LD_VERBOSE_SIZE) == 0)) { 1891 select |= SEL_ACT_LML; 1892 val = LML_FLG_TRC_VERBOSE; 1893 variable = ENV_FLG_VERBOSE; 1894 } 1895 } 1896 /* 1897 * LD_WARN (internal, used by ldd(1)). 1898 */ 1899 else if (*s1 == 'W') { 1900 if ((len == MSG_LD_WARN_SIZE) && (strncmp(s1, 1901 MSG_ORIG(MSG_LD_WARN), MSG_LD_WARN_SIZE) == 0)) { 1902 select |= SEL_ACT_LML; 1903 val = LML_FLG_TRC_WARN; 1904 variable = ENV_FLG_WARN; 1905 } 1906 #ifdef SIEBEL_DISABLE 1907 } 1908 /* 1909 * LD__FIX__ (undocumented, enable future technology that can't be 1910 * delivered in a patch release). 1911 */ 1912 else if (*s1 == '_') { 1913 if ((len == MSG_LD_FIX_1_SIZE) && (strncmp(s1, 1914 MSG_ORIG(MSG_LD_FIX_1), MSG_LD_FIX_1_SIZE) == 0)) { 1915 select |= SEL_ACT_RT; 1916 val = RT_FL_DISFIX_1; 1917 variable = ENV_FLG_FIX_1; 1918 } 1919 #endif 1920 } 1921 if (variable == 0) 1922 return; 1923 1924 /* 1925 * If the variable is already processed with ISA specific variable, 1926 * no further processing needed. 1927 */ 1928 if (((select & SEL_REPLACE) && (rplisa & variable)) || 1929 ((select & SEL_PERMANT) && (prmisa & variable))) 1930 return; 1931 1932 /* 1933 * Now mark the appropriate variables. 1934 * If the replaceable variable is already set, then the 1935 * process environment variable must be set. Any replaceable 1936 * variable specified in a configuration file can be ignored. 1937 */ 1938 if (env_flags & ENV_TYP_ISA) { 1939 /* 1940 * This is ISA setting. We do the setting 1941 * even if s2 is NULL. 1942 * If s2 is NULL, we might need to undo 1943 * the setting. 1944 */ 1945 if (select & SEL_REPLACE) { 1946 if (rplisa & variable) 1947 return; 1948 rplisa |= variable; 1949 } else { 1950 prmisa |= variable; 1951 } 1952 } else if (s2) { 1953 /* 1954 * This is non0-ISA setting 1955 */ 1956 if (select & SEL_REPLACE) { 1957 if (rplgen & variable) 1958 return; 1959 rplgen |= variable; 1960 } else 1961 prmgen |= variable; 1962 } else 1963 /* 1964 * This is non-ISA setting which 1965 * can be ignored. 1966 */ 1967 return; 1968 1969 /* 1970 * Now perform the setting. 1971 */ 1972 if (select & SEL_ACT_RT) { 1973 if (s2) 1974 rtld_flags |= val; 1975 else 1976 rtld_flags &= ~val; 1977 } else if (select & SEL_ACT_RT2) { 1978 if (s2) 1979 rtld_flags2 |= val; 1980 else 1981 rtld_flags2 &= ~val; 1982 } else if (select & SEL_ACT_STR) 1983 *str = s2; 1984 else if (select & SEL_ACT_LML) { 1985 if (s2) 1986 *lmflags |= val; 1987 else 1988 *lmflags &= ~val; 1989 } else if (select & SEL_ACT_LMLT) { 1990 if (s2) 1991 *lmtflags |= val; 1992 else 1993 *lmtflags &= ~val; 1994 } else if (select & SEL_ACT_SPEC_1) { 1995 /* 1996 * variable is either ENV_FLG_FLAGS or ENV_FLG_LIBPATH 1997 */ 1998 *str = s2; 1999 if ((select & SEL_REPLACE) && (env_flags & ENV_TYP_CONFIG)) { 2000 if (s2) { 2001 if (variable == ENV_FLG_FLAGS) 2002 env_info |= ENV_INF_FLAGCFG; 2003 else 2004 env_info |= ENV_INF_PATHCFG; 2005 } else { 2006 if (variable == ENV_FLG_FLAGS) 2007 env_info &= ~ENV_INF_FLAGCFG; 2008 else 2009 env_info &= ~ENV_INF_PATHCFG; 2010 } 2011 } 2012 } else if (select & SEL_ACT_SPEC_2) { 2013 /* 2014 * variables can be: ENV_FLG_ 2015 * AUDIT_ARGS, BINDING, CONCURRENCY, CONFGEN, 2016 * LOADFLTR, PROFILE, SIGNAL, TRACE_OBJS 2017 */ 2018 if (variable == ENV_FLG_AUDIT_ARGS) { 2019 if (s2) { 2020 audit_argcnt = atoi(s2); 2021 audit_argcnt += audit_argcnt % 2; 2022 } else 2023 audit_argcnt = 0; 2024 } else if (variable == ENV_FLG_BINDINGS) { 2025 if (s2) 2026 rpl_debug = MSG_ORIG(MSG_TKN_BINDINGS); 2027 else 2028 rpl_debug = 0; 2029 } else if (variable == ENV_FLG_CONCURRENCY) { 2030 if (s2) 2031 rtld_flags &= ~RT_FL_NOCONCUR; 2032 else 2033 rtld_flags |= RT_FL_NOCONCUR; 2034 } else if (variable == ENV_FLG_CONFGEN) { 2035 if (s2) { 2036 rtld_flags |= RT_FL_CONFGEN; 2037 *lmflags |= LML_FLG_IGNRELERR; 2038 } else { 2039 rtld_flags &= ~RT_FL_CONFGEN; 2040 *lmflags &= ~LML_FLG_IGNRELERR; 2041 } 2042 } else if (variable == ENV_FLG_LOADFLTR) { 2043 if (s2) { 2044 *lmtflags |= LML_TFLG_LOADFLTR; 2045 if (*s2 == '2') 2046 rtld_flags |= RT_FL_WARNFLTR; 2047 } else { 2048 *lmtflags &= ~LML_TFLG_LOADFLTR; 2049 rtld_flags &= ~RT_FL_WARNFLTR; 2050 } 2051 } else if (variable == ENV_FLG_PROFILE) { 2052 profile_name = s2; 2053 if (s2) { 2054 if (strcmp(s2, MSG_ORIG(MSG_FIL_RTLD)) == 0) { 2055 return; 2056 } 2057 /* BEGIN CSTYLED */ 2058 if (rtld_flags & RT_FL_SECURE) { 2059 profile_lib = 2060 #if defined(_ELF64) 2061 MSG_ORIG(MSG_PTH_LDPROFSE_64); 2062 #else 2063 MSG_ORIG(MSG_PTH_LDPROFSE); 2064 #endif 2065 } else { 2066 profile_lib = 2067 #if defined(_ELF64) 2068 MSG_ORIG(MSG_PTH_LDPROF_64); 2069 #else 2070 MSG_ORIG(MSG_PTH_LDPROF); 2071 #endif 2072 } 2073 /* END CSTYLED */ 2074 } else 2075 profile_lib = 0; 2076 } else if (variable == ENV_FLG_SIGNAL) { 2077 killsig = s2 ? atoi(s2) : SIGKILL; 2078 } else if (variable == ENV_FLG_TRACE_OBJS) { 2079 if (s2) { 2080 *lmflags |= LML_FLG_TRC_ENABLE; 2081 if (*s2 == '2') 2082 *lmflags |= LML_FLG_TRC_LDDSTUB; 2083 } else 2084 *lmflags &= 2085 ~(LML_FLG_TRC_ENABLE|LML_FLG_TRC_LDDSTUB); 2086 } 2087 } 2088 } 2089 2090 /* 2091 * Determine whether we have an architecture specific environment variable. 2092 * If we do, and we're the wrong architecture, it'll just get ignored. 2093 * Otherwise the variable is processed in it's architecture neutral form. 2094 */ 2095 static int 2096 ld_arch_env(const char *s1, size_t *len) 2097 { 2098 size_t _len = *len - 3; 2099 2100 if (s1[_len++] == '_') { 2101 if ((s1[_len] == '3') && (s1[_len + 1] == '2')) { 2102 #if defined(_ELF64) 2103 return (ENV_TYP_IGNORE); 2104 #else 2105 *len = *len - 3; 2106 return (ENV_TYP_ISA); 2107 #endif 2108 } 2109 if ((s1[_len] == '6') && (s1[_len + 1] == '4')) { 2110 #if defined(_ELF64) 2111 *len = *len - 3; 2112 return (ENV_TYP_ISA); 2113 #else 2114 return (ENV_TYP_IGNORE); 2115 #endif 2116 } 2117 } 2118 return (0); 2119 } 2120 2121 2122 /* 2123 * Process an LD_FLAGS environment variable. The value can be a comma 2124 * separated set of tokens, which are sent (in upper case) into the generic 2125 * LD_XXXX environment variable engine. For example: 2126 * 2127 * LD_FLAGS=bind_now -> LD_BIND_NOW=1 2128 * LD_FLAGS=library_path=/foo:. -> LD_LIBRARY_PATH=/foo:. 2129 * LD_FLAGS=debug=files:detail -> LD_DEBUG=files:detail 2130 * or 2131 * LD_FLAGS=bind_now,library_path=/foo:.,debug=files:detail 2132 */ 2133 static int 2134 ld_flags_env(const char *str, Word *lmflags, Word *lmtflags, 2135 uint_t env_flags, int aout) 2136 { 2137 char *nstr, *sstr, *estr = 0; 2138 size_t nlen, len; 2139 2140 if (str == 0) 2141 return (0); 2142 2143 /* 2144 * Create a new string as we're going to transform the token(s) into 2145 * uppercase and separate tokens with nulls. 2146 */ 2147 len = strlen(str); 2148 if ((nstr = malloc(len + 1)) == 0) 2149 return (1); 2150 (void) strcpy(nstr, str); 2151 2152 for (sstr = nstr; sstr; sstr++, len--) { 2153 int flags; 2154 2155 if ((*sstr != '\0') && (*sstr != ',')) { 2156 if (estr == 0) { 2157 if (*sstr == '=') 2158 estr = sstr; 2159 else { 2160 /* 2161 * Translate token to uppercase. Don't 2162 * use toupper(3C) as including this 2163 * code doubles the size of ld.so.1. 2164 */ 2165 if ((*sstr >= 'a') && (*sstr <= 'z')) 2166 *sstr = *sstr - ('a' - 'A'); 2167 } 2168 } 2169 continue; 2170 } 2171 2172 *sstr = '\0'; 2173 if (estr) { 2174 nlen = estr - nstr; 2175 if ((*++estr == '\0') || (*estr == ',')) 2176 estr = 0; 2177 } else 2178 nlen = sstr - nstr; 2179 2180 /* 2181 * Fabricate a boolean definition for any unqualified variable. 2182 * Thus LD_FLAGS=bind_now is represented as BIND_NOW=(null). 2183 * The value is sufficient to assert any boolean variables, plus 2184 * the term "(null)" is specifically chosen in case someone 2185 * mistakenly supplies something like LD_FLAGS=library_path. 2186 */ 2187 if (estr == 0) 2188 estr = (char *)MSG_INTL(MSG_STR_NULL); 2189 2190 /* 2191 * Determine whether the environment variable is 32- or 64-bit 2192 * specific. The length, len, will reflect the architecture 2193 * neutral portion of the string. 2194 */ 2195 if ((flags = ld_arch_env(nstr, &nlen)) != ENV_TYP_IGNORE) { 2196 ld_generic_env(nstr, nlen, estr, lmflags, 2197 lmtflags, (env_flags | flags), aout); 2198 } 2199 if (len == 0) 2200 return (0); 2201 2202 nstr = sstr + 1; 2203 estr = 0; 2204 } 2205 return (0); 2206 } 2207 2208 2209 /* 2210 * Process a single environment string. Only strings starting with `LD_' are 2211 * reserved for our use. By convention, all strings should be of the form 2212 * `LD_XXXX=', if the string is followed by a non-null value the appropriate 2213 * functionality is enabled. Also pick off applicable locale variables. 2214 */ 2215 #define LOC_LANG 1 2216 #define LOC_MESG 2 2217 #define LOC_ALL 3 2218 2219 static void 2220 ld_str_env(const char *s1, Word *lmflags, Word *lmtflags, uint_t env_flags, 2221 int aout) 2222 { 2223 const char *s2; 2224 static size_t loc = 0; 2225 2226 if (*s1++ != 'L') 2227 return; 2228 2229 /* 2230 * See if we have any locale environment settings. These environment 2231 * variables have a precedence, LC_ALL is higher than LC_MESSAGES which 2232 * is higher than LANG. 2233 */ 2234 s2 = s1; 2235 if ((*s2++ == 'C') && (*s2++ == '_') && (*s2 != '\0')) { 2236 if (strncmp(s2, MSG_ORIG(MSG_LC_ALL), MSG_LC_ALL_SIZE) == 0) { 2237 s2 += MSG_LC_ALL_SIZE; 2238 if ((*s2 != '\0') && (loc < LOC_ALL)) { 2239 glcs[CI_LCMESSAGES].lc_un.lc_ptr = (char *)s2; 2240 loc = LOC_ALL; 2241 } 2242 } else if (strncmp(s2, MSG_ORIG(MSG_LC_MESSAGES), 2243 MSG_LC_MESSAGES_SIZE) == 0) { 2244 s2 += MSG_LC_MESSAGES_SIZE; 2245 if ((*s2 != '\0') && (loc < LOC_MESG)) { 2246 glcs[CI_LCMESSAGES].lc_un.lc_ptr = (char *)s2; 2247 loc = LOC_MESG; 2248 } 2249 } 2250 return; 2251 } 2252 2253 s2 = s1; 2254 if ((*s2++ == 'A') && (*s2++ == 'N') && (*s2++ == 'G') && 2255 (*s2++ == '=') && (*s2 != '\0') && (loc < LOC_LANG)) { 2256 glcs[CI_LCMESSAGES].lc_un.lc_ptr = (char *)s2; 2257 loc = LOC_LANG; 2258 return; 2259 } 2260 2261 /* 2262 * Pick off any LD_XXXX environment variables. 2263 */ 2264 if ((*s1++ == 'D') && (*s1++ == '_') && (*s1 != '\0')) { 2265 size_t len; 2266 int flags; 2267 2268 /* 2269 * In a branded process we must ignore all LD_XXXX env vars 2270 * because they are intended for the brand's linker. 2271 * To affect the Solaris linker, use LD_BRAND_XXXX instead. 2272 */ 2273 if (rtld_flags2 & RT_FL2_BRANDED) { 2274 if (strncmp(s1, MSG_ORIG(MSG_LD_BRAND_PREFIX), 2275 MSG_LD_BRAND_PREFIX_SIZE) != 0) 2276 return; 2277 s1 += MSG_LD_BRAND_PREFIX_SIZE; 2278 } 2279 2280 /* 2281 * Environment variables with no value (ie. LD_XXXX=) typically 2282 * have no impact, however if environment variables are defined 2283 * within a configuration file, these null user settings can be 2284 * used to disable any configuration replaceable definitions. 2285 */ 2286 if ((s2 = strchr(s1, '=')) == 0) { 2287 len = strlen(s1); 2288 s2 = 0; 2289 } else if (*++s2 == '\0') { 2290 len = strlen(s1) - 1; 2291 s2 = 0; 2292 } else { 2293 len = s2 - s1 - 1; 2294 while (isspace(*s2)) 2295 s2++; 2296 } 2297 2298 /* 2299 * Determine whether the environment variable is 32- or 64-bit 2300 * specific. The length, len, will reflect the architecture 2301 * neutral portion of the string. 2302 */ 2303 if ((flags = ld_arch_env(s1, &len)) == ENV_TYP_IGNORE) 2304 return; 2305 env_flags |= flags; 2306 2307 ld_generic_env(s1, len, s2, lmflags, lmtflags, env_flags, aout); 2308 } 2309 } 2310 2311 /* 2312 * Internal getenv routine. Called immediately after ld.so.1 initializes 2313 * itself. 2314 */ 2315 int 2316 readenv_user(const char ** envp, Word *lmflags, Word *lmtflags, int aout) 2317 { 2318 char *locale; 2319 2320 if (envp == (const char **)0) 2321 return (0); 2322 2323 while (*envp != (const char *)0) 2324 ld_str_env(*envp++, lmflags, lmtflags, 0, aout); 2325 2326 /* 2327 * Having collected the best representation of any LD_FLAGS, process 2328 * these strings. 2329 */ 2330 if (ld_flags_env(rpl_ldflags, lmflags, lmtflags, 0, aout) == 1) 2331 return (1); 2332 2333 /* 2334 * Don't allow environment controlled auditing when tracing or if 2335 * explicitly disabled. Trigger all tracing modes from 2336 * LML_FLG_TRC_ENABLE. 2337 */ 2338 if ((*lmflags & LML_FLG_TRC_ENABLE) || (rtld_flags & RT_FL_NOAUDIT)) 2339 rpl_audit = profile_lib = profile_name = 0; 2340 if ((*lmflags & LML_FLG_TRC_ENABLE) == 0) 2341 *lmflags &= ~LML_MSK_TRC; 2342 2343 /* 2344 * If both LD_BIND_NOW and LD_BIND_LAZY are specified, the former wins. 2345 */ 2346 if ((rtld_flags2 & (RT_FL2_BINDNOW | RT_FL2_BINDLAZY)) == 2347 (RT_FL2_BINDNOW | RT_FL2_BINDLAZY)) 2348 rtld_flags2 &= ~RT_FL2_BINDLAZY; 2349 2350 /* 2351 * When using ldd(1) -r or -d against an executable, assert -p. 2352 */ 2353 if ((*lmflags & 2354 (LML_FLG_TRC_WARN | LML_FLG_TRC_LDDSTUB)) == LML_FLG_TRC_WARN) 2355 *lmflags |= LML_FLG_TRC_NOPAREXT; 2356 2357 /* 2358 * If we have a locale setting make sure its worth processing further. 2359 * C and POSIX locales don't need any processing. In addition, to 2360 * ensure no one escapes the /usr/lib/locale hierarchy, don't allow 2361 * the locale to contain a segment that leads upward in the file system 2362 * hierarchy (i.e. no '..' segments). Given that we'll be confined to 2363 * the /usr/lib/locale hierarchy, there is no need to extensively 2364 * validate the mode or ownership of any message file (as libc's 2365 * generic handling of message files does). Duplicate the string so 2366 * that new locale setting can generically cleanup any previous locales. 2367 */ 2368 if ((locale = glcs[CI_LCMESSAGES].lc_un.lc_ptr) != 0) { 2369 if (((*locale == 'C') && (*(locale + 1) == '\0')) || 2370 (strcmp(locale, MSG_ORIG(MSG_TKN_POSIX)) == 0) || 2371 (strstr(locale, MSG_ORIG(MSG_TKN_DOTDOT)) != NULL)) 2372 glcs[CI_LCMESSAGES].lc_un.lc_ptr = 0; 2373 else 2374 glcs[CI_LCMESSAGES].lc_un.lc_ptr = strdup(locale); 2375 } 2376 return (0); 2377 } 2378 2379 /* 2380 * Configuration environment processing. Called after the a.out has been 2381 * processed (as the a.out can specify its own configuration file). 2382 */ 2383 int 2384 readenv_config(Rtc_env * envtbl, Addr addr, int aout) 2385 { 2386 Word *lmflags = &(lml_main.lm_flags); 2387 Word *lmtflags = &(lml_main.lm_tflags); 2388 2389 if (envtbl == (Rtc_env *)0) 2390 return (0); 2391 2392 while (envtbl->env_str) { 2393 uint_t env_flags = ENV_TYP_CONFIG; 2394 2395 if (envtbl->env_flags & RTC_ENV_PERMANT) 2396 env_flags |= ENV_TYP_PERMANT; 2397 2398 ld_str_env((const char *)(envtbl->env_str + addr), 2399 lmflags, lmtflags, env_flags, 0); 2400 envtbl++; 2401 } 2402 2403 /* 2404 * Having collected the best representation of any LD_FLAGS, process 2405 * these strings. 2406 */ 2407 if (ld_flags_env(rpl_ldflags, lmflags, lmtflags, 0, aout) == 1) 2408 return (1); 2409 if (ld_flags_env(prm_ldflags, lmflags, lmtflags, ENV_TYP_CONFIG, 2410 aout) == 1) 2411 return (1); 2412 2413 /* 2414 * Don't allow environment controlled auditing when tracing or if 2415 * explicitly disabled. Trigger all tracing modes from 2416 * LML_FLG_TRC_ENABLE. 2417 */ 2418 if ((*lmflags & LML_FLG_TRC_ENABLE) || (rtld_flags & RT_FL_NOAUDIT)) 2419 prm_audit = profile_lib = profile_name = 0; 2420 if ((*lmflags & LML_FLG_TRC_ENABLE) == 0) 2421 *lmflags &= ~LML_MSK_TRC; 2422 2423 return (0); 2424 } 2425 2426 int 2427 dowrite(Prfbuf * prf) 2428 { 2429 /* 2430 * We do not have a valid file descriptor, so we are unable 2431 * to flush the buffer. 2432 */ 2433 if (prf->pr_fd == -1) 2434 return (0); 2435 (void) write(prf->pr_fd, prf->pr_buf, prf->pr_cur - prf->pr_buf); 2436 prf->pr_cur = prf->pr_buf; 2437 return (1); 2438 } 2439 2440 /* 2441 * Simplified printing. The following conversion specifications are supported: 2442 * 2443 * % [#] [-] [min field width] [. precision] s|d|x|c 2444 * 2445 * 2446 * dorprf takes the output buffer in the form of Prfbuf which permits 2447 * the verification of the output buffer size and the concatenation 2448 * of data to an already existing output buffer. The Prfbuf 2449 * structure contains the following: 2450 * 2451 * pr_buf pointer to the beginning of the output buffer. 2452 * pr_cur pointer to the next available byte in the output buffer. By 2453 * setting pr_cur ahead of pr_buf you can append to an already 2454 * existing buffer. 2455 * pr_len the size of the output buffer. By setting pr_len to '0' you 2456 * disable protection from overflows in the output buffer. 2457 * pr_fd a pointer to the file-descriptor the buffer will eventually be 2458 * output to. If pr_fd is set to '-1' then it's assumed there is 2459 * no output buffer, and doprf() will return with an error to 2460 * indicate an output buffer overflow. If pr_fd is > -1 then when 2461 * the output buffer is filled it will be flushed to pr_fd and will 2462 * then be available for additional data. 2463 */ 2464 #define FLG_UT_MINUS 0x0001 /* - */ 2465 #define FLG_UT_SHARP 0x0002 /* # */ 2466 #define FLG_UT_DOTSEEN 0x0008 /* dot appeared in format spec */ 2467 2468 /* 2469 * This macro is for use from within doprf only. It is to be used for checking 2470 * the output buffer size and placing characters into the buffer. 2471 */ 2472 #define PUTC(c) \ 2473 { \ 2474 char tmpc; \ 2475 \ 2476 tmpc = (c); \ 2477 if (bufsiz && (bp >= bufend)) { \ 2478 prf->pr_cur = bp; \ 2479 if (dowrite(prf) == 0) \ 2480 return (0); \ 2481 bp = prf->pr_cur; \ 2482 } \ 2483 *bp++ = tmpc; \ 2484 } 2485 2486 /* 2487 * Define a local buffer size for building a numeric value - large enough to 2488 * hold a 64-bit value. 2489 */ 2490 #define NUM_SIZE 22 2491 2492 size_t 2493 doprf(const char *format, va_list args, Prfbuf *prf) 2494 { 2495 char c; 2496 char *bp = prf->pr_cur; 2497 char *bufend = prf->pr_buf + prf->pr_len; 2498 size_t bufsiz = prf->pr_len; 2499 2500 while ((c = *format++) != '\0') { 2501 if (c != '%') { 2502 PUTC(c); 2503 } else { 2504 int base = 0, flag = 0, width = 0, prec = 0; 2505 size_t _i; 2506 int _c, _n; 2507 char *_s; 2508 int ls = 0; 2509 again: 2510 c = *format++; 2511 switch (c) { 2512 case '-': 2513 flag |= FLG_UT_MINUS; 2514 goto again; 2515 case '#': 2516 flag |= FLG_UT_SHARP; 2517 goto again; 2518 case '.': 2519 flag |= FLG_UT_DOTSEEN; 2520 goto again; 2521 case '0': 2522 case '1': 2523 case '2': 2524 case '3': 2525 case '4': 2526 case '5': 2527 case '6': 2528 case '7': 2529 case '8': 2530 case '9': 2531 if (flag & FLG_UT_DOTSEEN) 2532 prec = (prec * 10) + c - '0'; 2533 else 2534 width = (width * 10) + c - '0'; 2535 goto again; 2536 case 'x': 2537 case 'X': 2538 base = 16; 2539 break; 2540 case 'd': 2541 case 'D': 2542 case 'u': 2543 base = 10; 2544 flag &= ~FLG_UT_SHARP; 2545 break; 2546 case 'l': 2547 base = 10; 2548 ls++; /* number of l's (long or long long) */ 2549 if ((*format == 'l') || 2550 (*format == 'd') || (*format == 'D') || 2551 (*format == 'x') || (*format == 'X') || 2552 (*format == 'o') || (*format == 'O')) 2553 goto again; 2554 break; 2555 case 'o': 2556 case 'O': 2557 base = 8; 2558 break; 2559 case 'c': 2560 _c = va_arg(args, int); 2561 2562 for (_i = 24; _i > 0; _i -= 8) { 2563 if ((c = ((_c >> _i) & 0x7f)) != 0) { 2564 PUTC(c); 2565 } 2566 } 2567 if ((c = ((_c >> _i) & 0x7f)) != 0) { 2568 PUTC(c); 2569 } 2570 break; 2571 case 's': 2572 _s = va_arg(args, char *); 2573 _i = strlen(_s); 2574 /* LINTED */ 2575 _n = (int)(width - _i); 2576 if (!prec) 2577 /* LINTED */ 2578 prec = (int)_i; 2579 2580 if (width && !(flag & FLG_UT_MINUS)) { 2581 while (_n-- > 0) 2582 PUTC(' '); 2583 } 2584 while (((c = *_s++) != 0) && prec--) { 2585 PUTC(c); 2586 } 2587 if (width && (flag & FLG_UT_MINUS)) { 2588 while (_n-- > 0) 2589 PUTC(' '); 2590 } 2591 break; 2592 case '%': 2593 PUTC('%'); 2594 break; 2595 default: 2596 break; 2597 } 2598 2599 /* 2600 * Numeric processing 2601 */ 2602 if (base) { 2603 char local[NUM_SIZE]; 2604 size_t ssize = 0, psize = 0; 2605 const char *string = 2606 MSG_ORIG(MSG_STR_HEXNUM); 2607 const char *prefix = 2608 MSG_ORIG(MSG_STR_EMPTY); 2609 u_longlong_t num; 2610 2611 switch (ls) { 2612 case 0: /* int */ 2613 num = (u_longlong_t) 2614 va_arg(args, uint_t); 2615 break; 2616 case 1: /* long */ 2617 num = (u_longlong_t) 2618 va_arg(args, ulong_t); 2619 break; 2620 case 2: /* long long */ 2621 num = va_arg(args, u_longlong_t); 2622 break; 2623 } 2624 2625 if (flag & FLG_UT_SHARP) { 2626 if (base == 16) { 2627 prefix = MSG_ORIG(MSG_STR_HEX); 2628 psize = 2; 2629 } else { 2630 prefix = MSG_ORIG(MSG_STR_ZERO); 2631 psize = 1; 2632 } 2633 } 2634 if ((base == 10) && (long)num < 0) { 2635 prefix = MSG_ORIG(MSG_STR_NEGATE); 2636 psize = MSG_STR_NEGATE_SIZE; 2637 num = (u_longlong_t)(-(longlong_t)num); 2638 } 2639 2640 /* 2641 * Convert the numeric value into a local 2642 * string (stored in reverse order). 2643 */ 2644 _s = local; 2645 do { 2646 *_s++ = string[num % base]; 2647 num /= base; 2648 ssize++; 2649 } while (num); 2650 2651 ASSERT(ssize < sizeof (local)); 2652 2653 /* 2654 * Provide any precision or width padding. 2655 */ 2656 if (prec) { 2657 /* LINTED */ 2658 _n = (int)(prec - ssize); 2659 while ((_n-- > 0) && 2660 (ssize < sizeof (local))) { 2661 *_s++ = '0'; 2662 ssize++; 2663 } 2664 } 2665 if (width && !(flag & FLG_UT_MINUS)) { 2666 /* LINTED */ 2667 _n = (int)(width - ssize - psize); 2668 while (_n-- > 0) { 2669 PUTC(' '); 2670 } 2671 } 2672 2673 /* 2674 * Print any prefix and the numeric string 2675 */ 2676 while (*prefix) 2677 PUTC(*prefix++); 2678 do { 2679 PUTC(*--_s); 2680 } while (_s > local); 2681 2682 /* 2683 * Provide any width padding. 2684 */ 2685 if (width && (flag & FLG_UT_MINUS)) { 2686 /* LINTED */ 2687 _n = (int)(width - ssize - psize); 2688 while (_n-- > 0) 2689 PUTC(' '); 2690 } 2691 } 2692 } 2693 } 2694 2695 PUTC('\0'); 2696 prf->pr_cur = bp; 2697 return (1); 2698 } 2699 2700 static int 2701 doprintf(const char *format, va_list args, Prfbuf *prf) 2702 { 2703 char *ocur = prf->pr_cur; 2704 2705 if (doprf(format, args, prf) == 0) 2706 return (0); 2707 /* LINTED */ 2708 return ((int)(prf->pr_cur - ocur)); 2709 } 2710 2711 /* VARARGS2 */ 2712 int 2713 sprintf(char *buf, const char *format, ...) 2714 { 2715 va_list args; 2716 int len; 2717 Prfbuf prf; 2718 2719 va_start(args, format); 2720 prf.pr_buf = prf.pr_cur = buf; 2721 prf.pr_len = 0; 2722 prf.pr_fd = -1; 2723 len = doprintf(format, args, &prf); 2724 va_end(args); 2725 2726 /* 2727 * sprintf() return value excludes the terminating null byte. 2728 */ 2729 return (len - 1); 2730 } 2731 2732 /* VARARGS3 */ 2733 int 2734 snprintf(char *buf, size_t n, const char *format, ...) 2735 { 2736 va_list args; 2737 int len; 2738 Prfbuf prf; 2739 2740 va_start(args, format); 2741 prf.pr_buf = prf.pr_cur = buf; 2742 prf.pr_len = n; 2743 prf.pr_fd = -1; 2744 len = doprintf(format, args, &prf); 2745 va_end(args); 2746 2747 return (len); 2748 } 2749 2750 /* VARARGS2 */ 2751 int 2752 bufprint(Prfbuf *prf, const char *format, ...) 2753 { 2754 va_list args; 2755 int len; 2756 2757 va_start(args, format); 2758 len = doprintf(format, args, prf); 2759 va_end(args); 2760 2761 return (len); 2762 } 2763 2764 /*PRINTFLIKE1*/ 2765 int 2766 printf(const char *format, ...) 2767 { 2768 va_list args; 2769 char buffer[ERRSIZE]; 2770 Prfbuf prf; 2771 2772 va_start(args, format); 2773 prf.pr_buf = prf.pr_cur = buffer; 2774 prf.pr_len = ERRSIZE; 2775 prf.pr_fd = 1; 2776 (void) doprf(format, args, &prf); 2777 va_end(args); 2778 /* 2779 * Trim trailing '\0' form buffer 2780 */ 2781 prf.pr_cur--; 2782 return (dowrite(&prf)); 2783 } 2784 2785 static char errbuf[ERRSIZE], *nextptr = errbuf, *prevptr = 0; 2786 2787 /*PRINTFLIKE3*/ 2788 void 2789 eprintf(Lm_list *lml, Error error, const char *format, ...) 2790 { 2791 va_list args; 2792 int overflow = 0; 2793 static int lock = 0; 2794 Prfbuf prf; 2795 2796 if (lock || (nextptr == (errbuf + ERRSIZE))) 2797 return; 2798 2799 /* 2800 * Note: this lock is here to prevent the same thread from recursively 2801 * entering itself during a eprintf. ie: during eprintf malloc() fails 2802 * and we try and call eprintf ... and then malloc() fails .... 2803 */ 2804 lock = 1; 2805 2806 /* 2807 * If we have completed startup initialization, all error messages 2808 * must be saved. These are reported through dlerror(). If we're 2809 * still in the initialization stage, output the error directly and 2810 * add a newline. 2811 */ 2812 va_start(args, format); 2813 2814 prf.pr_buf = prf.pr_cur = nextptr; 2815 prf.pr_len = ERRSIZE - (nextptr - errbuf); 2816 2817 if (!(rtld_flags & RT_FL_APPLIC)) 2818 prf.pr_fd = 2; 2819 else 2820 prf.pr_fd = -1; 2821 2822 if (error > ERR_NONE) { 2823 if ((error == ERR_FATAL) && (rtld_flags2 & RT_FL2_FTL2WARN)) 2824 error = ERR_WARNING; 2825 if (error == ERR_WARNING) { 2826 if (err_strs[ERR_WARNING] == 0) 2827 err_strs[ERR_WARNING] = 2828 MSG_INTL(MSG_ERR_WARNING); 2829 } else if (error == ERR_FATAL) { 2830 if (err_strs[ERR_FATAL] == 0) 2831 err_strs[ERR_FATAL] = MSG_INTL(MSG_ERR_FATAL); 2832 } else if (error == ERR_ELF) { 2833 if (err_strs[ERR_ELF] == 0) 2834 err_strs[ERR_ELF] = MSG_INTL(MSG_ERR_ELF); 2835 } 2836 if (procname) { 2837 if (bufprint(&prf, MSG_ORIG(MSG_STR_EMSGFOR1), 2838 rtldname, procname, err_strs[error]) == 0) 2839 overflow = 1; 2840 } else { 2841 if (bufprint(&prf, MSG_ORIG(MSG_STR_EMSGFOR2), 2842 rtldname, err_strs[error]) == 0) 2843 overflow = 1; 2844 } 2845 if (overflow == 0) { 2846 /* 2847 * Remove the terminating '\0'. 2848 */ 2849 prf.pr_cur--; 2850 } 2851 } 2852 2853 if ((overflow == 0) && doprf(format, args, &prf) == 0) 2854 overflow = 1; 2855 2856 /* 2857 * If this is an ELF error, it will have been generated by a support 2858 * object that has a dependency on libelf. ld.so.1 doesn't generate any 2859 * ELF error messages as it doesn't interact with libelf. Determine the 2860 * ELF error string. 2861 */ 2862 if ((overflow == 0) && (error == ERR_ELF)) { 2863 static int (*elfeno)() = 0; 2864 static const char *(*elfemg)(); 2865 const char *emsg; 2866 Rt_map *dlmp, *lmp = lml_rtld.lm_head; 2867 2868 if (NEXT(lmp) && (elfeno == 0)) { 2869 if (((elfemg = (const char *(*)())dlsym_intn(RTLD_NEXT, 2870 MSG_ORIG(MSG_SYM_ELFERRMSG), lmp, &dlmp)) == 0) || 2871 ((elfeno = (int (*)())dlsym_intn(RTLD_NEXT, 2872 MSG_ORIG(MSG_SYM_ELFERRNO), lmp, &dlmp)) == 0)) 2873 elfeno = 0; 2874 } 2875 2876 /* 2877 * Lookup the message; equivalent to elf_errmsg(elf_errno()). 2878 */ 2879 if (elfeno && ((emsg = (* elfemg)((* elfeno)())) != 0)) { 2880 prf.pr_cur--; 2881 if (bufprint(&prf, MSG_ORIG(MSG_STR_EMSGFOR2), 2882 emsg) == 0) 2883 overflow = 1; 2884 } 2885 } 2886 2887 /* 2888 * Push out any message that's been built. Note, in the case of an 2889 * overflow condition, this message may be incomplete, in which case 2890 * make sure any partial string is null terminated. 2891 */ 2892 if (overflow) 2893 *(prf.pr_cur) = '\0'; 2894 if ((rtld_flags & (RT_FL_APPLIC | RT_FL_SILENCERR)) == 0) { 2895 *(prf.pr_cur - 1) = '\n'; 2896 (void) dowrite(&prf); 2897 } 2898 2899 DBG_CALL(Dbg_util_str(lml, nextptr)); 2900 va_end(args); 2901 2902 /* 2903 * Determine if there was insufficient space left in the buffer to 2904 * complete the message. If so, we'll have printed out as much as had 2905 * been processed if we're not yet executing the application. 2906 * Otherwise, there will be some debugging diagnostic indicating 2907 * as much of the error message as possible. Write out a final buffer 2908 * overflow diagnostic - unlocalized, so we don't chance more errors. 2909 */ 2910 if (overflow) { 2911 char *str = (char *)MSG_INTL(MSG_EMG_BUFOVRFLW); 2912 2913 if ((rtld_flags & RT_FL_SILENCERR) == 0) { 2914 lasterr = str; 2915 2916 if ((rtld_flags & RT_FL_APPLIC) == 0) { 2917 (void) write(2, str, strlen(str)); 2918 (void) write(2, MSG_ORIG(MSG_STR_NL), 2919 MSG_STR_NL_SIZE); 2920 } 2921 } 2922 DBG_CALL(Dbg_util_str(lml, str)); 2923 2924 lock = 0; 2925 nextptr = errbuf + ERRSIZE; 2926 return; 2927 } 2928 2929 /* 2930 * If the application has started, then error messages are being saved 2931 * for retrieval by dlerror(), or possible flushing from rtldexit() in 2932 * the case of a fatal error. In this case, establish the next error 2933 * pointer. If we haven't started the application, the whole message 2934 * buffer can be reused. 2935 */ 2936 if ((rtld_flags & RT_FL_SILENCERR) == 0) { 2937 lasterr = nextptr; 2938 2939 /* 2940 * Note, should we encounter an error such as ENOMEM, there may 2941 * be a number of the same error messages (ie. an operation 2942 * fails with ENOMEM, and then the attempts to construct the 2943 * error message itself, which incurs additional ENOMEM errors). 2944 * Compare any previous error message with the one we've just 2945 * created to prevent any duplication clutter. 2946 */ 2947 if ((rtld_flags & RT_FL_APPLIC) && 2948 ((prevptr == 0) || (strcmp(prevptr, nextptr) != 0))) { 2949 prevptr = nextptr; 2950 nextptr = prf.pr_cur; 2951 *nextptr = '\0'; 2952 } 2953 } 2954 lock = 0; 2955 } 2956 2957 2958 #if DEBUG 2959 /* 2960 * Provide assfail() for ASSERT() statements, 2961 * see <sys/debug.h> for further details. 2962 */ 2963 int 2964 assfail(const char *a, const char *f, int l) 2965 { 2966 (void) printf("assertion failed: %s, file: %s, line: %d\n", a, f, l); 2967 (void) _lwp_kill(_lwp_self(), SIGABRT); 2968 return (0); 2969 } 2970 #endif 2971 2972 /* 2973 * Exit. If we arrive here with a non zero status it's because of a fatal 2974 * error condition (most commonly a relocation error). If the application has 2975 * already had control, then the actual fatal error message will have been 2976 * recorded in the dlerror() message buffer. Print the message before really 2977 * exiting. 2978 */ 2979 void 2980 rtldexit(Lm_list * lml, int status) 2981 { 2982 if (status) { 2983 if (rtld_flags & RT_FL_APPLIC) { 2984 /* 2985 * If the error buffer has been used, write out all 2986 * pending messages - lasterr is simply a pointer to 2987 * the last message in this buffer. However, if the 2988 * buffer couldn't be created at all, lasterr points 2989 * to a constant error message string. 2990 */ 2991 if (*errbuf) { 2992 char *errptr = errbuf; 2993 char *errend = errbuf + ERRSIZE; 2994 2995 while ((errptr < errend) && *errptr) { 2996 size_t size = strlen(errptr); 2997 (void) write(2, errptr, size); 2998 (void) write(2, MSG_ORIG(MSG_STR_NL), 2999 MSG_STR_NL_SIZE); 3000 errptr += (size + 1); 3001 } 3002 } 3003 if (lasterr && ((lasterr < errbuf) || 3004 (lasterr > (errbuf + ERRSIZE)))) { 3005 (void) write(2, lasterr, strlen(lasterr)); 3006 (void) write(2, MSG_ORIG(MSG_STR_NL), 3007 MSG_STR_NL_SIZE); 3008 } 3009 } 3010 leave(lml); 3011 (void) _lwp_kill(_lwp_self(), killsig); 3012 } 3013 _exit(status); 3014 } 3015 3016 /* 3017 * Routines to co-ordinate the opening of /dev/zero and /proc. 3018 * dz_fd is exported for possible use by libld.so, and to insure it gets 3019 * closed on leaving ld.so.1. 3020 */ 3021 int dz_fd = FD_UNAVAIL; 3022 3023 void 3024 dz_init(int fd) 3025 { 3026 dz_fd = fd; 3027 } 3028 3029 3030 /* 3031 * mmap() a page from MAP_ANON 3032 * 3033 * Note: MAP_ANON is only on Solaris8++, we use this routine to 3034 * not only mmap(MAP_ANON) but to also probe if it is available 3035 * on the current OS. 3036 */ 3037 Am_ret 3038 anon_map(Lm_list *lml, caddr_t *addr, size_t len, int prot, int flags) 3039 { 3040 #if defined(MAP_ANON) 3041 static int noanon = 0; 3042 caddr_t va; 3043 3044 if (noanon == 0) { 3045 if ((va = (caddr_t)mmap(*addr, len, prot, 3046 (flags | MAP_ANON), -1, 0)) != MAP_FAILED) { 3047 *addr = va; 3048 return (AM_OK); 3049 } 3050 3051 if ((errno != EBADF) && (errno != EINVAL)) { 3052 int err = errno; 3053 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_MMAPANON), 3054 MSG_ORIG(MSG_PTH_DEVZERO), strerror(err)); 3055 return (AM_ERROR); 3056 } else 3057 noanon = 1; 3058 } 3059 #endif 3060 return (AM_NOSUP); 3061 } 3062 3063 /* 3064 * Map anonymous memory from /dev/zero, or via MAP_ANON. 3065 * 3066 * (MAP_ANON only appears on Solaris 8, so we need fall-back 3067 * behavior for older systems.) 3068 */ 3069 caddr_t 3070 dz_map(Lm_list *lml, caddr_t addr, size_t len, int prot, int flags) 3071 { 3072 caddr_t va; 3073 int err; 3074 Am_ret amret; 3075 3076 amret = anon_map(lml, &addr, len, prot, flags); 3077 3078 if (amret == AM_OK) 3079 return (addr); 3080 if (amret == AM_ERROR) 3081 return (MAP_FAILED); 3082 3083 /* amret == AM_NOSUP -> fallback to a devzero mmaping */ 3084 3085 if (dz_fd == FD_UNAVAIL) { 3086 if ((dz_fd = open(MSG_ORIG(MSG_PTH_DEVZERO), 3087 O_RDONLY)) == FD_UNAVAIL) { 3088 err = errno; 3089 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), 3090 MSG_ORIG(MSG_PTH_DEVZERO), strerror(err)); 3091 return (MAP_FAILED); 3092 } 3093 } 3094 3095 if ((va = mmap(addr, len, prot, flags, dz_fd, 0)) == MAP_FAILED) { 3096 err = errno; 3097 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_MMAP), 3098 MSG_ORIG(MSG_PTH_DEVZERO), strerror(err)); 3099 } 3100 return (va); 3101 } 3102 3103 static int pr_fd = FD_UNAVAIL; 3104 3105 int 3106 pr_open(Lm_list *lml) 3107 { 3108 char proc[16]; 3109 3110 if (pr_fd == FD_UNAVAIL) { 3111 (void) snprintf(proc, 16, MSG_ORIG(MSG_FMT_PROC), 3112 (int)getpid()); 3113 if ((pr_fd = open(proc, O_RDONLY)) == FD_UNAVAIL) { 3114 int err = errno; 3115 3116 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), proc, 3117 strerror(err)); 3118 } 3119 } 3120 return (pr_fd); 3121 } 3122 3123 static int nu_fd = FD_UNAVAIL; 3124 3125 caddr_t 3126 nu_map(Lm_list *lml, caddr_t addr, size_t len, int prot, int flags) 3127 { 3128 caddr_t va; 3129 int err; 3130 3131 if (nu_fd == FD_UNAVAIL) { 3132 if ((nu_fd = open(MSG_ORIG(MSG_PTH_DEVNULL), 3133 O_RDONLY)) == FD_UNAVAIL) { 3134 err = errno; 3135 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), 3136 MSG_ORIG(MSG_PTH_DEVNULL), strerror(err)); 3137 return (MAP_FAILED); 3138 } 3139 } 3140 3141 if ((va = (caddr_t)mmap(addr, len, prot, flags, nu_fd, 0)) == 3142 MAP_FAILED) { 3143 err = errno; 3144 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_MMAP), 3145 MSG_ORIG(MSG_PTH_DEVNULL), strerror(err)); 3146 } 3147 return (va); 3148 } 3149 3150 /* 3151 * Generic entry point from user code - simply grabs a lock, and bumps the 3152 * entrance count. 3153 */ 3154 int 3155 enter(void) 3156 { 3157 if (rt_bind_guard(THR_FLG_RTLD)) { 3158 (void) rt_mutex_lock(&rtldlock); 3159 if (rtld_flags & RT_FL_OPERATION) 3160 ld_entry_cnt++; 3161 return (1); 3162 } 3163 return (0); 3164 } 3165 3166 /* 3167 * Determine whether a search path has been used. 3168 */ 3169 static void 3170 is_path_used(Lm_list *lml, Word unref, int *nl, Pnode *pnp, const char *obj) 3171 { 3172 for (; pnp; pnp = pnp->p_next) { 3173 const char *fmt, *name; 3174 3175 if ((pnp->p_len == 0) || (pnp->p_orig & PN_FLG_USED)) 3176 continue; 3177 3178 /* 3179 * If this pathname originated from an expanded token, use the 3180 * original for any diagnostic output. 3181 */ 3182 if ((name = pnp->p_oname) == 0) 3183 name = pnp->p_name; 3184 3185 if (unref == 0) { 3186 if ((*nl)++ == 0) 3187 DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD)); 3188 DBG_CALL(Dbg_unused_path(lml, name, pnp->p_orig, 3189 (pnp->p_orig & PN_FLG_DUPLICAT), obj)); 3190 continue; 3191 } 3192 3193 /* 3194 * For now, disable any unused search path processing that has 3195 * been triggered for ldd(1). 3196 */ 3197 if (unref) 3198 continue; 3199 3200 if (pnp->p_orig & LA_SER_LIBPATH) { 3201 if (pnp->p_orig & LA_SER_CONFIG) { 3202 if (pnp->p_orig & PN_FLG_DUPLICAT) 3203 fmt = MSG_INTL(MSG_DUP_LDLIBPATHC); 3204 else 3205 fmt = MSG_INTL(MSG_USD_LDLIBPATHC); 3206 } else { 3207 if (pnp->p_orig & PN_FLG_DUPLICAT) 3208 fmt = MSG_INTL(MSG_DUP_LDLIBPATH); 3209 else 3210 fmt = MSG_INTL(MSG_USD_LDLIBPATH); 3211 } 3212 } else if (pnp->p_orig & LA_SER_RUNPATH) { 3213 fmt = MSG_INTL(MSG_USD_RUNPATH); 3214 } else 3215 continue; 3216 3217 if ((*nl)++ == 0) 3218 (void) printf(MSG_ORIG(MSG_STR_NL)); 3219 (void) printf(fmt, name, obj); 3220 } 3221 } 3222 3223 /* 3224 * Generate diagnostics as to whether an object has been used. A symbolic 3225 * reference that gets bound to an object marks it as used. Dependencies that 3226 * are unused when RTLD_NOW is in effect should be removed from future builds 3227 * of an object. Dependencies that are unused without RTLD_NOW in effect are 3228 * candidates for lazy-loading. 3229 * 3230 * Unreferenced objects identify objects that are defined as dependencies but 3231 * are unreferenced by the caller. These unreferenced objects may however be 3232 * referenced by other objects within the process, and therefore don't qualify 3233 * as completely unused. They are still an unnecessary overhead. 3234 * 3235 * Unreferenced runpaths are also captured under ldd -U, or "unused,detail" 3236 * debugging. 3237 */ 3238 void 3239 unused(Lm_list *lml) 3240 { 3241 Rt_map *lmp; 3242 int nl = 0; 3243 Word unref, unuse; 3244 3245 /* 3246 * If we're not tracing unused references or dependencies, or debugging 3247 * there's nothing to do. 3248 */ 3249 unref = lml->lm_flags & LML_FLG_TRC_UNREF; 3250 unuse = lml->lm_flags & LML_FLG_TRC_UNUSED; 3251 3252 if ((unref == 0) && (unuse == 0) && (DBG_ENABLED == 0)) 3253 return; 3254 3255 /* 3256 * Detect unused global search paths. 3257 */ 3258 if (rpl_libdirs) 3259 is_path_used(lml, unref, &nl, rpl_libdirs, config->c_name); 3260 if (prm_libdirs) 3261 is_path_used(lml, unref, &nl, prm_libdirs, config->c_name); 3262 3263 nl = 0; 3264 lmp = lml->lm_head; 3265 if (RLIST(lmp)) 3266 is_path_used(lml, unref, &nl, RLIST(lmp), NAME(lmp)); 3267 3268 /* 3269 * Traverse the link-maps looking for unreferenced or unused 3270 * dependencies. Ignore the first object on a link-map list, as this 3271 * is always used. 3272 */ 3273 nl = 0; 3274 for (lmp = (Rt_map *)NEXT(lmp); lmp; lmp = (Rt_map *)NEXT(lmp)) { 3275 /* 3276 * Determine if this object contains any runpaths that have 3277 * not been used. 3278 */ 3279 if (RLIST(lmp)) 3280 is_path_used(lml, unref, &nl, RLIST(lmp), NAME(lmp)); 3281 3282 /* 3283 * If tracing unreferenced objects, or under debugging, 3284 * determine whether any of this objects callers haven't 3285 * referenced it. 3286 */ 3287 if (unref || DBG_ENABLED) { 3288 Bnd_desc *bdp; 3289 Aliste idx; 3290 3291 for (APLIST_TRAVERSE(CALLERS(lmp), idx, bdp)) { 3292 Rt_map *clmp; 3293 3294 if (bdp->b_flags & BND_REFER) 3295 continue; 3296 3297 clmp = bdp->b_caller; 3298 if (FLAGS1(clmp) & FL1_RT_LDDSTUB) 3299 continue; 3300 3301 /* BEGIN CSTYLED */ 3302 if (nl++ == 0) { 3303 if (unref) 3304 (void) printf(MSG_ORIG(MSG_STR_NL)); 3305 else 3306 DBG_CALL(Dbg_util_nl(lml, 3307 DBG_NL_STD)); 3308 } 3309 3310 if (unref) 3311 (void) printf(MSG_INTL(MSG_LDD_UNREF_FMT), 3312 NAME(lmp), NAME(clmp)); 3313 else 3314 DBG_CALL(Dbg_unused_unref(lmp, NAME(clmp))); 3315 /* END CSTYLED */ 3316 } 3317 } 3318 3319 /* 3320 * If tracing unused objects simply display those objects that 3321 * haven't been referenced by anyone. 3322 */ 3323 if (FLAGS1(lmp) & FL1_RT_USED) 3324 continue; 3325 3326 if (nl++ == 0) { 3327 if (unref || unuse) 3328 (void) printf(MSG_ORIG(MSG_STR_NL)); 3329 else 3330 DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD)); 3331 } 3332 if (CYCGROUP(lmp)) { 3333 if (unref || unuse) 3334 (void) printf(MSG_INTL(MSG_LDD_UNCYC_FMT), 3335 NAME(lmp), CYCGROUP(lmp)); 3336 else 3337 DBG_CALL(Dbg_unused_file(lml, NAME(lmp), 0, 3338 CYCGROUP(lmp))); 3339 } else { 3340 if (unref || unuse) 3341 (void) printf(MSG_INTL(MSG_LDD_UNUSED_FMT), 3342 NAME(lmp)); 3343 else 3344 DBG_CALL(Dbg_unused_file(lml, NAME(lmp), 0, 0)); 3345 } 3346 } 3347 3348 DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD)); 3349 } 3350 3351 /* 3352 * Initialization routine for the Fmap structure. If the fmap structure is 3353 * already in use, any mapping is released. The structure is then initialized 3354 * in preparation for further use. 3355 */ 3356 void 3357 fmap_setup() 3358 { 3359 #if defined(MAP_ALIGN) 3360 /* 3361 * If MAP_ALIGN is set, the fm_addr has been seeded with an alignment 3362 * value. Otherwise, if fm_addr is non-null it indicates a mapping that 3363 * should now be freed. 3364 */ 3365 if (fmap->fm_maddr && ((fmap->fm_mflags & MAP_ALIGN) == 0)) 3366 (void) munmap((caddr_t)fmap->fm_maddr, fmap->fm_msize); 3367 3368 /* 3369 * Providing we haven't determined that this system doesn't support 3370 * MAP_ALIGN, initialize the mapping address with the default segment 3371 * alignment. 3372 */ 3373 if ((rtld_flags2 & RT_FL2_NOMALIGN) == 0) { 3374 fmap->fm_maddr = (char *)M_SEGM_ALIGN; 3375 fmap->fm_mflags = MAP_PRIVATE | MAP_ALIGN; 3376 } else { 3377 fmap->fm_maddr = 0; 3378 fmap->fm_mflags = MAP_PRIVATE; 3379 } 3380 #else 3381 if (fmap->fm_maddr) 3382 (void) munmap((caddr_t)fmap->fm_maddr, fmap->fm_msize); 3383 3384 fmap->fm_maddr = 0; 3385 fmap->fm_mflags = MAP_PRIVATE; 3386 #endif 3387 3388 fmap->fm_msize = FMAP_SIZE; 3389 fmap->fm_hwptr = 0; 3390 } 3391 3392 /* 3393 * Generic cleanup routine called prior to returning control to the user. 3394 * Insures that any ld.so.1 specific file descriptors or temporary mapping are 3395 * released, and any locks dropped. 3396 */ 3397 void 3398 leave(Lm_list *lml) 3399 { 3400 Lm_list *elml = lml; 3401 Rt_map *clmp; 3402 Aliste idx; 3403 3404 /* 3405 * Alert the debuggers that the link-maps are consistent. Note, in the 3406 * case of tearing down a whole link-map list, lml will be null. In 3407 * this case use the main link-map list to test for a notification. 3408 */ 3409 if (elml == 0) 3410 elml = &lml_main; 3411 if (elml->lm_flags & LML_FLG_DBNOTIF) 3412 rd_event(elml, RD_DLACTIVITY, RT_CONSISTENT); 3413 3414 /* 3415 * Alert any auditors that the link-maps are consistent. 3416 */ 3417 for (APLIST_TRAVERSE(elml->lm_actaudit, idx, clmp)) { 3418 audit_activity(clmp, LA_ACT_CONSISTENT); 3419 3420 aplist_delete(elml->lm_actaudit, &idx); 3421 } 3422 3423 if (dz_fd != FD_UNAVAIL) { 3424 (void) close(dz_fd); 3425 dz_fd = FD_UNAVAIL; 3426 } 3427 3428 if (pr_fd != FD_UNAVAIL) { 3429 (void) close(pr_fd); 3430 pr_fd = FD_UNAVAIL; 3431 } 3432 3433 if (nu_fd != FD_UNAVAIL) { 3434 (void) close(nu_fd); 3435 nu_fd = FD_UNAVAIL; 3436 } 3437 3438 fmap_setup(); 3439 3440 /* 3441 * Reinitialize error message pointer, and any overflow indication. 3442 */ 3443 nextptr = errbuf; 3444 prevptr = 0; 3445 3446 /* 3447 * Don't drop our lock if we are running on our link-map list as 3448 * there's little point in doing so since we are single-threaded. 3449 * 3450 * LML_FLG_HOLDLOCK is set for: 3451 * *) The ld.so.1's link-map list. 3452 * *) The auditor's link-map if the environment is 3453 * libc/libthread un-unified. 3454 */ 3455 if (lml && (lml->lm_flags & LML_FLG_HOLDLOCK)) 3456 return; 3457 3458 if (rt_bind_clear(0) & THR_FLG_RTLD) { 3459 (void) rt_mutex_unlock(&rtldlock); 3460 (void) rt_bind_clear(THR_FLG_RTLD); 3461 } 3462 } 3463 3464 int 3465 callable(Rt_map *clmp, Rt_map *dlmp, Grp_hdl *ghp, uint_t slflags) 3466 { 3467 APlist *calp, *dalp; 3468 Aliste idx1, idx2; 3469 Grp_hdl *ghp1, *ghp2; 3470 3471 /* 3472 * An object can always find symbols within itself. 3473 */ 3474 if (clmp == dlmp) 3475 return (1); 3476 3477 /* 3478 * The search for a singleton must look in every loaded object. 3479 */ 3480 if (slflags & LKUP_SINGLETON) 3481 return (1); 3482 3483 /* 3484 * Don't allow an object to bind to an object that is being deleted 3485 * unless the binder is also being deleted. 3486 */ 3487 if ((FLAGS(dlmp) & FLG_RT_DELETE) && 3488 ((FLAGS(clmp) & FLG_RT_DELETE) == 0)) 3489 return (0); 3490 3491 /* 3492 * An object with world access can always bind to an object with global 3493 * visibility. 3494 */ 3495 if ((MODE(clmp) & RTLD_WORLD) && (MODE(dlmp) & RTLD_GLOBAL)) 3496 return (1); 3497 3498 /* 3499 * An object with local access can only bind to an object that is a 3500 * member of the same group. 3501 */ 3502 if (((MODE(clmp) & RTLD_GROUP) == 0) || 3503 ((calp = GROUPS(clmp)) == NULL) || ((dalp = GROUPS(dlmp)) == NULL)) 3504 return (0); 3505 3506 /* 3507 * Traverse the list of groups the caller is a part of. 3508 */ 3509 for (APLIST_TRAVERSE(calp, idx1, ghp1)) { 3510 /* 3511 * If we're testing for the ability of two objects to bind to 3512 * each other regardless of a specific group, ignore that group. 3513 */ 3514 if (ghp && (ghp1 == ghp)) 3515 continue; 3516 3517 /* 3518 * Traverse the list of groups the destination is a part of. 3519 */ 3520 for (APLIST_TRAVERSE(dalp, idx2, ghp2)) { 3521 Grp_desc *gdp; 3522 Aliste idx3; 3523 3524 if (ghp1 != ghp2) 3525 continue; 3526 3527 /* 3528 * Make sure the relationship between the destination 3529 * and the caller provide symbols for relocation. 3530 * Parents are maintained as callers, but unless the 3531 * destination object was opened with RTLD_PARENT, the 3532 * parent doesn't provide symbols for the destination 3533 * to relocate against. 3534 */ 3535 for (ALIST_TRAVERSE(ghp2->gh_depends, idx3, gdp)) { 3536 if (dlmp != gdp->gd_depend) 3537 continue; 3538 3539 if (gdp->gd_flags & GPD_RELOC) 3540 return (1); 3541 } 3542 } 3543 } 3544 return (0); 3545 } 3546 3547 /* 3548 * Initialize the environ symbol. Traditionally this is carried out by the crt 3549 * code prior to jumping to main. However, init sections get fired before this 3550 * variable is initialized, so ld.so.1 sets this directly from the AUX vector 3551 * information. In addition, a process may have multiple link-maps (ld.so.1's 3552 * debugging and preloading objects), and link auditing, and each may need an 3553 * environ variable set. 3554 * 3555 * This routine is called after a relocation() pass, and thus provides for: 3556 * 3557 * o setting environ on the main link-map after the initial application and 3558 * its dependencies have been established. Typically environ lives in the 3559 * application (provided by its crt), but in older applications it might 3560 * be in libc. Who knows what's expected of applications not built on 3561 * Solaris. 3562 * 3563 * o after loading a new shared object. We can add shared objects to various 3564 * link-maps, and any link-map dependencies requiring getopt() require 3565 * their own environ. In addition, lazy loading might bring in the 3566 * supplier of environ (libc used to be a lazy loading candidate) after 3567 * the link-map has been established and other objects are present. 3568 * 3569 * This routine handles all these scenarios, without adding unnecessary overhead 3570 * to ld.so.1. 3571 */ 3572 void 3573 set_environ(Lm_list *lml) 3574 { 3575 Rt_map *dlmp; 3576 Sym *sym; 3577 Slookup sl; 3578 uint_t binfo; 3579 3580 /* 3581 * Initialize the symbol lookup data structure. 3582 */ 3583 SLOOKUP_INIT(sl, MSG_ORIG(MSG_SYM_ENVIRON), lml->lm_head, lml->lm_head, 3584 ld_entry_cnt, 0, 0, 0, 0, LKUP_WEAK); 3585 3586 if (sym = LM_LOOKUP_SYM(lml->lm_head)(&sl, &dlmp, &binfo, 0)) { 3587 lml->lm_environ = (char ***)sym->st_value; 3588 3589 if (!(FLAGS(dlmp) & FLG_RT_FIXED)) 3590 lml->lm_environ = 3591 (char ***)((uintptr_t)lml->lm_environ + 3592 (uintptr_t)ADDR(dlmp)); 3593 *(lml->lm_environ) = (char **)environ; 3594 lml->lm_flags |= LML_FLG_ENVIRON; 3595 } 3596 } 3597 3598 /* 3599 * Determine whether we have a secure executable. Uid and gid information 3600 * can be passed to us via the aux vector, however if these values are -1 3601 * then use the appropriate system call to obtain them. 3602 * 3603 * o If the user is the root they can do anything 3604 * 3605 * o If the real and effective uid's don't match, or the real and 3606 * effective gid's don't match then this is determined to be a `secure' 3607 * application. 3608 * 3609 * This function is called prior to any dependency processing (see _setup.c). 3610 * Any secure setting will remain in effect for the life of the process. 3611 */ 3612 void 3613 security(uid_t uid, uid_t euid, gid_t gid, gid_t egid, int auxflags) 3614 { 3615 #ifdef AT_SUN_AUXFLAGS 3616 if (auxflags != -1) { 3617 if ((auxflags & AF_SUN_SETUGID) != 0) 3618 rtld_flags |= RT_FL_SECURE; 3619 return; 3620 } 3621 #endif 3622 if (uid == (uid_t)-1) 3623 uid = getuid(); 3624 if (uid) { 3625 if (euid == (uid_t)-1) 3626 euid = geteuid(); 3627 if (uid != euid) 3628 rtld_flags |= RT_FL_SECURE; 3629 else { 3630 if (gid == (gid_t)-1) 3631 gid = getgid(); 3632 if (egid == (gid_t)-1) 3633 egid = getegid(); 3634 if (gid != egid) 3635 rtld_flags |= RT_FL_SECURE; 3636 } 3637 } 3638 } 3639 3640 /* 3641 * _REENTRANT code gets errno redefined to a function so provide for return 3642 * of the thread errno if applicable. This has no meaning in ld.so.1 which 3643 * is basically singled threaded. Provide the interface for our dependencies. 3644 */ 3645 #undef errno 3646 #pragma weak _private___errno = ___errno 3647 int * 3648 ___errno() 3649 { 3650 extern int errno; 3651 3652 return (&errno); 3653 } 3654 3655 /* 3656 * The interface with the c library which is supplied through libdl.so.1. 3657 * A non-null argument allows a function pointer array to be passed to us which 3658 * is used to re-initialize the linker libc table. 3659 */ 3660 void 3661 _ld_libc(void * ptr) 3662 { 3663 get_lcinterface(_caller(caller(), CL_EXECDEF), (Lc_interface *)ptr); 3664 } 3665 3666 /* 3667 * Determine whether a symbol name should be demangled. 3668 */ 3669 const char * 3670 demangle(const char *name) 3671 { 3672 if (rtld_flags & RT_FL_DEMANGLE) 3673 return (conv_demangle_name(name)); 3674 else 3675 return (name); 3676 } 3677