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