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