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