xref: /onnv-gate/usr/src/cmd/sgs/rtld/common/external.c (revision 13093:48f2dbca79a2)
11824Srie /*
21824Srie  * CDDL HEADER START
31824Srie  *
41824Srie  * The contents of this file are subject to the terms of the
51824Srie  * Common Development and Distribution License (the "License").
61824Srie  * You may not use this file except in compliance with the License.
71824Srie  *
81824Srie  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91824Srie  * or http://www.opensolaris.org/os/licensing.
101824Srie  * See the License for the specific language governing permissions
111824Srie  * and limitations under the License.
121824Srie  *
131824Srie  * When distributing Covered Code, include this CDDL HEADER in each
141824Srie  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151824Srie  * If applicable, add the following below this CDDL HEADER, with the
161824Srie  * fields enclosed by brackets "[]" replaced with your own identifying
171824Srie  * information: Portions Copyright [yyyy] [name of copyright owner]
181824Srie  *
191824Srie  * CDDL HEADER END
201824Srie  */
211824Srie 
221824Srie /*
2312877SRod.Evans@Sun.COM  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
241824Srie  */
255891Sraf 
261824Srie /*
271824Srie  * Implementation of all external interfaces between ld.so.1 and libc.
281824Srie  *
291824Srie  * This file started as a set of routines that provided synchronization and
308883SRod.Evans@Sun.COM  * locking operations using calls to libthread.  libthread has merged with libc
318883SRod.Evans@Sun.COM  * under the Unified Process Model (UPM), and things have gotten a lot simpler.
328883SRod.Evans@Sun.COM  * This file continues to establish and redirect various events within ld.so.1
338883SRod.Evans@Sun.COM  * to interfaces within libc.
341824Srie  *
351824Srie  * Until libc is loaded and relocated, any external interfaces are captured
361824Srie  * locally.  Each link-map list maintains its own set of external vectors, as
371824Srie  * each link-map list typically provides its own libc.  Although this per-link-
381824Srie  * map list vectoring provides a degree of flexibility, there is a protocol
391824Srie  * expected when calling various libc interfaces.
401824Srie  *
411824Srie  * i.	Any new alternative link-map list should call CI_THRINIT, and then call
421824Srie  *	CI_TLS_MODADD to register any TLS for each object of that link-map list
431824Srie  *	(this item is labeled i. as auditors can be the first objects loaded,
441824Srie  *	and they exist on their own lik-map list).
451824Srie  *
461824Srie  * ii.	For the primary link-map list, CI_TLS_STATMOD must be called first to
471824Srie  *	register any static TLS.  This routine is called regardless of there
481824Srie  *	being any TLS, as this routine also establishes the link-map list as the
491824Srie  *	primary list and fixes the association of uberdata).  CI_THRINIT should
501824Srie  *	then be called.
511824Srie  *
521824Srie  * iii.	Any objects added to an existing link-map list (primary or alternative)
531824Srie  *	should call CI_TLS_MODADD to register any additional TLS.
541824Srie  *
551824Srie  * These events are established by:
561824Srie  *
571824Srie  * i.	Typically, libc is loaded as part of the primary dependencies of any
581824Srie  *	link-map list (since the Unified Process Model (UPM), libc can't be
591824Srie  *	lazily loaded).  To minimize the possibility of loading and registering
601824Srie  *	objects, and then tearing them down (because of a relocation error),
611824Srie  *	external vectors are established as part of load_completion().  This
621824Srie  *	routine is called on completion of any operation that can cause objects
631824Srie  *	to be loaded.  This point of control insures the objects have been fully
641824Srie  *	analyzed and relocated, and moved to their controlling link-map list.
651824Srie  *	The external vectors are established prior to any .inits being fired.
661824Srie  *
671824Srie  * ii.	Calls to CI_THRINIT, and CI_TLS_MODADD also occur as part of
681824Srie  *	load_completion().  CI_THRINIT is only called once for each link-map
691824Srie  *	control list.
701824Srie  *
711824Srie  * iii.	Calls to CI_TLS_STATMOD, and CI_THRINIT occur for the primary link-map
721824Srie  *	list in the final stages of setup().
731824Srie  *
741824Srie  * The interfaces provide by libc can be divided into two families.  The first
751824Srie  * family consists of those interfaces that should be called from the link-map
761824Srie  * list.  It's possible that these interfaces convey state concerning the
771824Srie  * link-map list they are part of:
781824Srie  *
791824Srie  *	CI_ATEXIT
801824Srie  *	CI TLS_MODADD
811824Srie  *	CI_TLS_MODREM
821824Srie  *	CI_TLS_STATMOD
831824Srie  *	CI_THRINIT
841824Srie  *
851824Srie  * The second family are global in nature, that is, the link-map list from
861824Srie  * which they are called provides no state information.  In fact, for
871824Srie  * CI_BIND_GUARD, the calling link-map isn't even known.  The link-map can only
881824Srie  * be deduced after ld.so.1's global lock has been obtained.  Therefore, the
891824Srie  * following interfaces are also maintained as global:
901824Srie  *
911824Srie  *	CI_LCMESSAGES
921824Srie  *	CI_BIND_GUARD
931824Srie  *	CI_BIND_CLEAR
941824Srie  *	CI_THR_SELF
951824Srie  *
961824Srie  * Note, it is possible that these global interfaces are obtained from an
971824Srie  * alternative link-map list that gets torn down because of a processing
981824Srie  * failure (unlikely, because the link-map list components must be analyzed
991824Srie  * and relocated prior to load_completion(), but perhaps the tear down is still
1001824Srie  * a possibility).  Thus the global interfaces may have to be replaced.  Once
1011824Srie  * the interfaces have been obtained from the primary link-map, they can
1021824Srie  * remain fixed, as the primary link-map isn't going to go anywhere.
1031824Srie  *
1041824Srie  * The last wrinkle in the puzzle is what happens if an alternative link-map
1051824Srie  * is loaded with no libc dependency?  In this case, the alternative objects
1061824Srie  * can not call CI_THRINIT, can not be allowed to use TLS, and will not receive
1071824Srie  * any atexit processing.
1081824Srie  *
1091824Srie  * The history of these external interfaces is defined by their version:
1101824Srie  *
1111824Srie  * TI_VERSION == 1
1121824Srie  *	Under this model libthread provided rw_rwlock/rw_unlock, through which
1131824Srie  *	all rt_mutex_lock/rt_mutex_unlock calls were vectored.
1141824Srie  *	Under libc/libthread these interfaces provided _sigon/_sigoff (unlike
1151824Srie  *	lwp/libthread that provided signal blocking via bind_guard/bind_clear).
1161824Srie  *
1171824Srie  * TI_VERSION == 2
1181824Srie  *	Under this model only libthreads bind_guard/bind_clear and thr_self
1191824Srie  *	interfaces were used.  Both libthreads blocked signals under the
1201824Srie  *	bind_guard/bind_clear interfaces.   Lower level locking is derived
1211824Srie  *	from internally bound _lwp_ interfaces.  This removes recursive
1221824Srie  *	problems encountered when obtaining locking interfaces from libthread.
1231824Srie  *	The use of mutexes over reader/writer locks also enables the use of
1241824Srie  *	condition variables for controlling thread concurrency (allows access
1251824Srie  *	to objects only after their .init has completed).
1261824Srie  *
1271824Srie  * NOTE, the TI_VERSION indicated the ti_interface version number, where the
1281824Srie  * ti_interface was a large vector of functions passed to both libc (to override
1291824Srie  * the thread stub interfaces) and ld.so.1.  ld.so.1 used only a small subset of
1301824Srie  * these interfaces.
1311824Srie  *
1321824Srie  * CI_VERSION == 1
1331824Srie  *	Introduced with CI_VERSION & CI_ATEXIT
1341824Srie  *
1351824Srie  * CI_VERSION == 2 (Solaris 8 update 2).
1361824Srie  *	Added support for CI_LCMESSAGES
1371824Srie  *
1381824Srie  * CI_VERSION == 3 (Solaris 9).
1391824Srie  *	Added the following versions to the CI table:
1401824Srie  *
1411824Srie  *		CI_BIND_GUARD, CI_BIND_CLEAR, CI_THR_SELF
1421824Srie  *		CI_TLS_MODADD, CI_TLS_MOD_REMOVE, CI_TLS_STATMOD
1431824Srie  *
1441824Srie  *	This version introduced the DT_SUNW_RTLDINFO structure as a mechanism
1451824Srie  *	to handshake with ld.so.1.
1461824Srie  *
1471824Srie  * CI_VERSION == 4 (Solaris 10).
1481824Srie  *	Added the CI_THRINIT handshake as part of the libc/libthread unified
1491824Srie  *	process model.  libc now initializes the current thread pointer from
1501824Srie  *	this interface (and no longer relies on the INITFIRST flag - which
1511824Srie  *	others have started to camp out on).
1521824Srie  *
1539569SRod.Evans@Sun.COM  * CI_VERSION == 5 (Solaris 11).
1549569SRod.Evans@Sun.COM  *	Use of "protected" references within libc, so that symbols are
1559569SRod.Evans@Sun.COM  *	pre-bound, and don't require ld.so.1 binding.  This implementation
1569569SRod.Evans@Sun.COM  *	protects libc's critical regions from being vectored to auditors.
1579569SRod.Evans@Sun.COM  *
1589569SRod.Evans@Sun.COM  * CI_VERSION == 6 (Solaris 11).
1599569SRod.Evans@Sun.COM  *	Added the CI_CRITICAL handshake, to allow "mem*" family to be reexposed
1609569SRod.Evans@Sun.COM  *	as "global", and thus be redirected to auxiliary filters.
1619569SRod.Evans@Sun.COM  *
1621824Srie  * Release summary:
1631824Srie  *
1641824Srie  *	Solaris 8	CI_ATEXIT via _ld_libc()
1651824Srie  *			TI_* via _ld_concurrency()
1661824Srie  *
1671824Srie  *	Solaris 9	CI_ATEXIT and CI_LCMESSAGES via _ld_libc()
1681824Srie  *			CI_* via RTLDINFO and _ld_libc()  - new libthread
1691824Srie  *			TI_* via _ld_concurrency()  - old libthread
1701824Srie  *
1711824Srie  *	Solaris 10	CI_ATEXIT and CI_LCMESSAGES via _ld_libc()
1721824Srie  *			CI_* via RTLDINFO and _ld_libc()  - new libthread
1731824Srie  */
1741824Srie 
1755891Sraf #include <sys/debug.h>
1765891Sraf #include <synch.h>
1775891Sraf #include <signal.h>
1785891Sraf #include <thread.h>
1795891Sraf #include <synch.h>
1805891Sraf #include <strings.h>
1815891Sraf #include <stdio.h>
1825891Sraf #include <debug.h>
1835891Sraf #include <libc_int.h>
1845891Sraf #include "_elf.h"
1855891Sraf #include "_rtld.h"
1861824Srie 
1871824Srie /*
1881824Srie  * This interface provides the unified process model communication between
1898883SRod.Evans@Sun.COM  * ld.so.1 and libc.  This interface can be called a number of times:
1908883SRod.Evans@Sun.COM  *
1918883SRod.Evans@Sun.COM  *   -	Initially, this interface is called to process RTLDINFO.  This data
1928883SRod.Evans@Sun.COM  *	structure is typically provided by libc, and contains the address of
1938883SRod.Evans@Sun.COM  *	libc interfaces that must be called to initialize threads information.
1948883SRod.Evans@Sun.COM  *
1958883SRod.Evans@Sun.COM  *   -	_ld_libc(), this interface can also be called by libc at process
1968883SRod.Evans@Sun.COM  *	initialization, after libc has been loaded and relocated, but before
1978883SRod.Evans@Sun.COM  *	control has been passed to any user code (.init's or main()).  This
1988883SRod.Evans@Sun.COM  *	call provides additional libc interface information that ld.so.1 must
1998883SRod.Evans@Sun.COM  *	call during process execution.
2008883SRod.Evans@Sun.COM  *
2018883SRod.Evans@Sun.COM  *   -	_ld_libc() can also be called by libc during process execution to
2028883SRod.Evans@Sun.COM  * 	re-establish interfaces such as the locale.
2031824Srie  */
2048883SRod.Evans@Sun.COM static void
get_lcinterface(Rt_map * lmp,Lc_interface * funcs)2051824Srie get_lcinterface(Rt_map *lmp, Lc_interface *funcs)
2061824Srie {
2078883SRod.Evans@Sun.COM 	int		threaded = 0, entry = 0, tag;
2081824Srie 	Lm_list		*lml;
2091824Srie 	Lc_desc		*lcp;
2101824Srie 
2118883SRod.Evans@Sun.COM 	if ((lmp == NULL) || (funcs == NULL))
2121824Srie 		return;
2131824Srie 
2148883SRod.Evans@Sun.COM 	/*
2158883SRod.Evans@Sun.COM 	 * Once the process is active, ensure we grab a lock.
2168883SRod.Evans@Sun.COM 	 */
2178883SRod.Evans@Sun.COM 	if (rtld_flags & RT_FL_APPLIC)
2188883SRod.Evans@Sun.COM 		entry = enter(0);
2198883SRod.Evans@Sun.COM 
2201824Srie 	lml = LIST(lmp);
2211824Srie 	lcp = &lml->lm_lcs[0];
2221824Srie 
2231824Srie 	DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
2241824Srie 
2251824Srie 	for (tag = funcs->ci_tag; tag; tag = (++funcs)->ci_tag) {
2261824Srie 		char	*gptr;
2271824Srie 		char	*lptr = funcs->ci_un.ci_ptr;
2281824Srie 
2291824Srie 		DBG_CALL(Dbg_util_lcinterface(lmp, tag, lptr));
2301824Srie 
2311824Srie 		if (tag >= CI_MAX)
2321824Srie 			continue;
2331824Srie 
2341824Srie 		/*
2351824Srie 		 * Maintain all interfaces on a per-link-map basis.  Note, for
2361824Srie 		 * most interfaces, only the first interface is used for any
2371824Srie 		 * link-map list.  This prevents accidents with developers who
2381824Srie 		 * manage to load two different versions of libc.
2391824Srie 		 */
2401824Srie 		if ((lcp[tag].lc_lmp) &&
2411824Srie 		    (tag != CI_LCMESSAGES) && (tag != CI_VERSION)) {
2421824Srie 			DBG_CALL(Dbg_unused_lcinterface(lmp,
2431824Srie 			    lcp[tag].lc_lmp, tag));
2441824Srie 			continue;
2451824Srie 		}
2461824Srie 
2471824Srie 		lcp[tag].lc_un.lc_ptr = lptr;
2481824Srie 		lcp[tag].lc_lmp = lmp;
2491824Srie 
2501824Srie 		gptr = glcs[tag].lc_un.lc_ptr;
2511824Srie 
2521824Srie 		/*
2531824Srie 		 * Process any interfaces that must be maintained on a global
2541824Srie 		 * basis.
2551824Srie 		 */
2561824Srie 		switch (tag) {
2571824Srie 		case CI_ATEXIT:
2581824Srie 			break;
2591824Srie 
2601824Srie 		case CI_LCMESSAGES:
2611824Srie 			/*
2621824Srie 			 * At startup, ld.so.1 can establish a locale from one
2631824Srie 			 * of the locale family of environment variables (see
2641824Srie 			 * ld_str_env() and readenv_user()).  During process
2651824Srie 			 * execution the locale can also be changed by the user.
2661824Srie 			 * This interface is called from libc should the locale
2671824Srie 			 * be modified.  Presently, only one global locale is
2681824Srie 			 * maintained for all link-map lists, and only objects
2691824Srie 			 * on the primrary link-map may change this locale.
2701824Srie 			 */
2711824Srie 			if ((lml->lm_flags & LML_FLG_BASELM) &&
2728883SRod.Evans@Sun.COM 			    ((gptr == NULL) || (strcmp(gptr, lptr) != 0))) {
2731824Srie 				/*
2741824Srie 				 * If we've obtained a message locale (typically
2751824Srie 				 * supplied via libc's setlocale()), then
2761824Srie 				 * register the locale for use in dgettext() so
2771824Srie 				 * as to reestablish the locale for ld.so.1's
2781824Srie 				 * messages.
2791824Srie 				 */
2801824Srie 				if (gptr) {
2811824Srie 					free((void *)gptr);
2821824Srie 					rtld_flags |= RT_FL_NEWLOCALE;
2831824Srie 				}
2841824Srie 				glcs[tag].lc_un.lc_ptr = strdup(lptr);
2851824Srie 
2861824Srie 				/*
2871824Srie 				 * Clear any cached messages.
2881824Srie 				 */
2898883SRod.Evans@Sun.COM 				bzero(err_strs, sizeof (err_strs));
2908883SRod.Evans@Sun.COM 				nosym_str = NULL;
2911824Srie 			}
2921824Srie 			break;
2931824Srie 
2941824Srie 		case CI_BIND_GUARD:
2951824Srie 		case CI_BIND_CLEAR:
2961824Srie 		case CI_THR_SELF:
2979569SRod.Evans@Sun.COM 		case CI_CRITICAL:
2981824Srie 			/*
2991824Srie 			 * If the global vector is unset, or this is the primary
3001824Srie 			 * link-map, set the global vector.
3011824Srie 			 */
3028883SRod.Evans@Sun.COM 			if ((gptr == NULL) || (lml->lm_flags & LML_FLG_BASELM))
3031824Srie 				glcs[tag].lc_un.lc_ptr = lptr;
3041824Srie 
3051824Srie 			/* FALLTHROUGH */
3061824Srie 
3071824Srie 		case CI_TLS_MODADD:
3081824Srie 		case CI_TLS_MODREM:
3091824Srie 		case CI_TLS_STATMOD:
3101824Srie 		case CI_THRINIT:
3111824Srie 			threaded++;
3121824Srie 			break;
3131824Srie 
3141824Srie 		case CI_VERSION:
3151824Srie 			if ((rtld_flags2 & RT_FL2_RTLDSEEN) == 0) {
3169131SRod.Evans@Sun.COM 				Aliste	idx;
3179131SRod.Evans@Sun.COM 				Lm_list	*lml2;
3189131SRod.Evans@Sun.COM 				int	version;
3198883SRod.Evans@Sun.COM 
3201824Srie 				rtld_flags2 |= RT_FL2_RTLDSEEN;
3211824Srie 
3226515Sraf 				version = funcs->ci_un.ci_val;
3236515Sraf #if defined(CI_V_FIVE)
3246515Sraf 				if (version >= CI_V_FIVE) {
3256515Sraf 					thr_flg_nolock = THR_FLG_NOLOCK;
3266515Sraf 					thr_flg_reenter = THR_FLG_REENTER;
3276515Sraf 				}
3286515Sraf #endif
3298883SRod.Evans@Sun.COM 				if (version < CI_V_FOUR)
3301824Srie 					break;
3311824Srie 
3328883SRod.Evans@Sun.COM 				rtld_flags2 |= RT_FL2_UNIFPROC;
3331824Srie 
3348883SRod.Evans@Sun.COM 				/*
3358883SRod.Evans@Sun.COM 				 * We might have seen an auditor which is not
3368883SRod.Evans@Sun.COM 				 * dependent on libc.  Such an auditor's link
3378883SRod.Evans@Sun.COM 				 * map list has LML_FLG_HOLDLOCK set.  This
3388883SRod.Evans@Sun.COM 				 * lock needs to be dropped.  Refer to
3398883SRod.Evans@Sun.COM 				 * audit_setup() in audit.c.
3408883SRod.Evans@Sun.COM 				 */
3418883SRod.Evans@Sun.COM 				if ((rtld_flags2 & RT_FL2_HASAUDIT) == 0)
3428883SRod.Evans@Sun.COM 					break;
3438883SRod.Evans@Sun.COM 
3448883SRod.Evans@Sun.COM 				/*
3459131SRod.Evans@Sun.COM 				 * Yes, we did.  Take care of them.
3468883SRod.Evans@Sun.COM 				 */
3479131SRod.Evans@Sun.COM 				for (APLIST_TRAVERSE(dynlm_list, idx, lml2)) {
3488883SRod.Evans@Sun.COM 					Rt_map *map = (Rt_map *)lml2->lm_head;
3498883SRod.Evans@Sun.COM 
3508883SRod.Evans@Sun.COM 					if (FLAGS(map) & FLG_RT_AUDIT) {
3518883SRod.Evans@Sun.COM 						lml2->lm_flags &=
3528883SRod.Evans@Sun.COM 						    ~LML_FLG_HOLDLOCK;
3531824Srie 					}
3541824Srie 				}
3551824Srie 			}
3561824Srie 			break;
3571824Srie 
3581824Srie 		default:
3591824Srie 			break;
3601824Srie 		}
3611824Srie 	}
3621824Srie 
3638883SRod.Evans@Sun.COM 	if (threaded) {
3648883SRod.Evans@Sun.COM 		/*
3658883SRod.Evans@Sun.COM 		 * If a version of libc gives us only a subset of the TLS
3668883SRod.Evans@Sun.COM 		 * interfaces, it's confused and we discard the whole lot.
3678883SRod.Evans@Sun.COM 		 */
3688883SRod.Evans@Sun.COM 		if ((lcp[CI_TLS_MODADD].lc_un.lc_func &&
3698883SRod.Evans@Sun.COM 		    lcp[CI_TLS_MODREM].lc_un.lc_func &&
3708883SRod.Evans@Sun.COM 		    lcp[CI_TLS_STATMOD].lc_un.lc_func) == NULL) {
3718883SRod.Evans@Sun.COM 			lcp[CI_TLS_MODADD].lc_un.lc_func = NULL;
3728883SRod.Evans@Sun.COM 			lcp[CI_TLS_MODREM].lc_un.lc_func = NULL;
3738883SRod.Evans@Sun.COM 			lcp[CI_TLS_STATMOD].lc_un.lc_func = NULL;
3748883SRod.Evans@Sun.COM 		}
3751824Srie 
3768883SRod.Evans@Sun.COM 		/*
3778883SRod.Evans@Sun.COM 		 * Indicate that we're now thread capable.
3788883SRod.Evans@Sun.COM 		 */
3798883SRod.Evans@Sun.COM 		if ((lml->lm_flags & LML_FLG_RTLDLM) == 0)
3808883SRod.Evans@Sun.COM 			rtld_flags |= RT_FL_THREADS;
3811824Srie 	}
3821824Srie 
3838883SRod.Evans@Sun.COM 	if (entry)
3848883SRod.Evans@Sun.COM 		leave(lml, 0);
3851824Srie }
3861824Srie 
3871824Srie /*
3881824Srie  * At this point we know we have a set of objects that have been fully analyzed
3891824Srie  * and relocated.  Prior to the next major step of running .init sections (ie.
3901824Srie  * running user code), retrieve any RTLDINFO interfaces.
3911824Srie  */
3921824Srie int
rt_get_extern(Lm_list * lml,Rt_map * lmp)3931824Srie rt_get_extern(Lm_list *lml, Rt_map *lmp)
3941824Srie {
3951824Srie 	if (lml->lm_rti) {
3965892Sab196087 		Aliste		idx;
3971824Srie 		Rti_desc	*rti;
3981824Srie 
3995892Sab196087 		for (ALIST_TRAVERSE(lml->lm_rti, idx, rti))
4001824Srie 			get_lcinterface(rti->rti_lmp, rti->rti_info);
4011824Srie 
4021824Srie 		free(lml->lm_rti);
4031824Srie 		lml->lm_rti = 0;
4041824Srie 	}
4051824Srie 
4061824Srie 	/*
4071824Srie 	 * Perform some sanity checks.  If we have TLS requirements we better
4081824Srie 	 * have the associated external interfaces.
4091824Srie 	 */
4108883SRod.Evans@Sun.COM 	if (lml->lm_tls &&
4118883SRod.Evans@Sun.COM 	    (lml->lm_lcs[CI_TLS_STATMOD].lc_un.lc_func == NULL)) {
4122145Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_TLS_NOSUPPORT),
4131824Srie 		    NAME(lmp));
4141824Srie 		return (0);
4151824Srie 	}
4161824Srie 	return (1);
4171824Srie }
4181824Srie 
4198883SRod.Evans@Sun.COM /*
4208883SRod.Evans@Sun.COM  * Provide an interface for libc to communicate additional interface
4218883SRod.Evans@Sun.COM  * information.
4228883SRod.Evans@Sun.COM  */
4238883SRod.Evans@Sun.COM void
_ld_libc(void * ptr)4248883SRod.Evans@Sun.COM _ld_libc(void *ptr)
4258883SRod.Evans@Sun.COM {
4268883SRod.Evans@Sun.COM 	get_lcinterface(_caller(caller(), CL_EXECDEF), (Lc_interface *)ptr);
4278883SRod.Evans@Sun.COM }
4288883SRod.Evans@Sun.COM 
4291824Srie static int	bindmask = 0;
4301824Srie 
4311824Srie int
rt_bind_guard(int flags)4326515Sraf rt_bind_guard(int flags)
4331824Srie {
4341824Srie 	int	(*fptr)(int);
4356515Sraf 	int	bindflag;
4361824Srie 
4371824Srie 	if ((fptr = glcs[CI_BIND_GUARD].lc_un.lc_func) != NULL) {
4386515Sraf 		return ((*fptr)(flags));
4391824Srie 	} else {
4406515Sraf 		bindflag = (flags & THR_FLG_RTLD);
4411824Srie 		if ((bindflag & bindmask) == 0) {
4421824Srie 			bindmask |= bindflag;
4431824Srie 			return (1);
4441824Srie 		}
4451824Srie 		return (0);
4461824Srie 	}
4471824Srie }
4481824Srie 
4491824Srie int
rt_bind_clear(int flags)4506515Sraf rt_bind_clear(int flags)
4511824Srie {
4521824Srie 	int	(*fptr)(int);
4536515Sraf 	int	bindflag;
4541824Srie 
4551824Srie 	if ((fptr = glcs[CI_BIND_CLEAR].lc_un.lc_func) != NULL) {
4566515Sraf 		return ((*fptr)(flags));
4571824Srie 	} else {
4586515Sraf 		bindflag = (flags & THR_FLG_RTLD);
4591824Srie 		if (bindflag == 0)
4601824Srie 			return (bindmask);
4611824Srie 		else {
4621824Srie 			bindmask &= ~bindflag;
4631824Srie 			return (0);
4641824Srie 		}
4651824Srie 	}
4661824Srie }
4671824Srie 
4681824Srie /*
4691824Srie  * Make sure threads have been initialized.  This interface is called once for
4701824Srie  * each link-map list.
4711824Srie  */
4721824Srie void
rt_thr_init(Lm_list * lml)4731824Srie rt_thr_init(Lm_list *lml)
4741824Srie {
4751824Srie 	void	(*fptr)(void);
4761824Srie 
4778883SRod.Evans@Sun.COM 	if ((fptr =
4788883SRod.Evans@Sun.COM 	    (void (*)())lml->lm_lcs[CI_THRINIT].lc_un.lc_func) != NULL) {
4798883SRod.Evans@Sun.COM 		lml->lm_lcs[CI_THRINIT].lc_un.lc_func = NULL;
48012877SRod.Evans@Sun.COM 
48112877SRod.Evans@Sun.COM 		leave(lml, thr_flg_reenter);
4821824Srie 		(*fptr)();
4836515Sraf 		(void) enter(thr_flg_reenter);
4849340SRod.Evans@Sun.COM 
4859340SRod.Evans@Sun.COM 		/*
4869340SRod.Evans@Sun.COM 		 * If this is an alternative link-map list, and this is the
4879340SRod.Evans@Sun.COM 		 * first call to initialize threads, don't let the destination
4889340SRod.Evans@Sun.COM 		 * libc be deleted.  It is possible that an auditors complete
4899340SRod.Evans@Sun.COM 		 * initialization fails, but there is presently no main link-map
4909340SRod.Evans@Sun.COM 		 * list.  As this libc has established the thread pointer, don't
4919340SRod.Evans@Sun.COM 		 * delete this libc, otherwise the initialization of libc on the
4929340SRod.Evans@Sun.COM 		 * main link-map can be compromised during its threads
4939340SRod.Evans@Sun.COM 		 * initialization.
4949340SRod.Evans@Sun.COM 		 */
4959340SRod.Evans@Sun.COM 		if (((lml->lm_flags & LML_FLG_BASELM) == 0) &&
4969340SRod.Evans@Sun.COM 		    ((rtld_flags2 & RT_FL2_PLMSETUP) == 0))
4979340SRod.Evans@Sun.COM 			MODE(lml->lm_lcs[CI_THRINIT].lc_lmp) |= RTLD_NODELETE;
4981824Srie 	}
4991824Srie }
5001824Srie 
5011824Srie thread_t
rt_thr_self()5021824Srie rt_thr_self()
5031824Srie {
5041824Srie 	thread_t	(*fptr)(void);
5051824Srie 
5061824Srie 	if ((fptr = (thread_t (*)())glcs[CI_THR_SELF].lc_un.lc_func) != NULL)
5071824Srie 		return ((*fptr)());
5081824Srie 
5091824Srie 	return (1);
5101824Srie }
5111824Srie 
5121824Srie int
rt_mutex_lock(Rt_lock * mp)5139569SRod.Evans@Sun.COM rt_mutex_lock(Rt_lock *mp)
5141824Srie {
5151824Srie 	return (_lwp_mutex_lock((lwp_mutex_t *)mp));
5161824Srie }
5171824Srie 
5181824Srie int
rt_mutex_unlock(Rt_lock * mp)5199569SRod.Evans@Sun.COM rt_mutex_unlock(Rt_lock *mp)
5201824Srie {
5211824Srie 	return (_lwp_mutex_unlock((lwp_mutex_t *)mp));
5221824Srie }
5231824Srie 
5241824Srie /*
5259569SRod.Evans@Sun.COM  * Test whether we're in a libc critical region.  Certain function references,
5269569SRod.Evans@Sun.COM  * like the "mem*" family, might require binding.  Although these functions can
5279569SRod.Evans@Sun.COM  * safely bind to auxiliary filtees, they should not be captured by auditors.
5289569SRod.Evans@Sun.COM  */
5299569SRod.Evans@Sun.COM int
rt_critical()5309569SRod.Evans@Sun.COM rt_critical()
5319569SRod.Evans@Sun.COM {
5329569SRod.Evans@Sun.COM 	int	(*fptr)(void);
5339569SRod.Evans@Sun.COM 
5349569SRod.Evans@Sun.COM 	if ((fptr = glcs[CI_CRITICAL].lc_un.lc_func) != NULL)
5359569SRod.Evans@Sun.COM 		return ((*fptr)());
5369569SRod.Evans@Sun.COM 
5379569SRod.Evans@Sun.COM 	return (0);
5389569SRod.Evans@Sun.COM }
5399569SRod.Evans@Sun.COM 
5409569SRod.Evans@Sun.COM /*
5411824Srie  * Mutex interfaces to resolve references from any objects extracted from
5421824Srie  * libc_pic.a.  Note, as ld.so.1 is essentially single threaded these can be
5431824Srie  * noops.
5441824Srie  */
5456812Sraf #pragma weak lmutex_lock = mutex_lock
5461824Srie /* ARGSUSED */
5471824Srie int
mutex_lock(mutex_t * mp)5486812Sraf mutex_lock(mutex_t *mp)
5491824Srie {
5501824Srie 	return (0);
5511824Srie }
5521824Srie 
5536812Sraf #pragma weak lmutex_unlock = mutex_unlock
5541824Srie /* ARGSUSED */
5551824Srie int
mutex_unlock(mutex_t * mp)5566812Sraf mutex_unlock(mutex_t *mp)
5571824Srie {
5581824Srie 	return (0);
5591824Srie }
5601824Srie 
5613453Sraf /* ARGSUSED */
5623453Sraf int
mutex_init(mutex_t * mp,int type,void * arg)5636812Sraf mutex_init(mutex_t *mp, int type, void *arg)
5643453Sraf {
5653453Sraf 	return (0);
5663453Sraf }
5673453Sraf 
5683453Sraf /* ARGSUSED */
5693453Sraf int
mutex_destroy(mutex_t * mp)5706812Sraf mutex_destroy(mutex_t *mp)
5713453Sraf {
5723453Sraf 	return (0);
5733453Sraf }
5743453Sraf 
5751824Srie /*
5761824Srie  * This is needed to satisfy sysconf() (case _SC_THREAD_STACK_MIN)
5771824Srie  */
5781824Srie size_t
thr_min_stack()5796812Sraf thr_min_stack()
5801824Srie {
58112877SRod.Evans@Sun.COM 	return (sizeof (uintptr_t) * 1024);
5821824Srie }
5831824Srie 
5845891Sraf /*
585*13093SRoger.Faulkner@Oracle.COM  * Local str[n]casecmp() interfaces for the dynamic linker,
586*13093SRoger.Faulkner@Oracle.COM  * to avoid problems when linking with libc_pic.a
587*13093SRoger.Faulkner@Oracle.COM  */
588*13093SRoger.Faulkner@Oracle.COM int
strcasecmp(const char * s1,const char * s2)589*13093SRoger.Faulkner@Oracle.COM strcasecmp(const char *s1, const char *s2)
590*13093SRoger.Faulkner@Oracle.COM {
591*13093SRoger.Faulkner@Oracle.COM 	extern int ascii_strcasecmp(const char *, const char *);
592*13093SRoger.Faulkner@Oracle.COM 
593*13093SRoger.Faulkner@Oracle.COM 	return (ascii_strcasecmp(s1, s2));
594*13093SRoger.Faulkner@Oracle.COM }
595*13093SRoger.Faulkner@Oracle.COM 
596*13093SRoger.Faulkner@Oracle.COM int
strncasecmp(const char * s1,const char * s2,size_t n)597*13093SRoger.Faulkner@Oracle.COM strncasecmp(const char *s1, const char *s2, size_t n)
598*13093SRoger.Faulkner@Oracle.COM {
599*13093SRoger.Faulkner@Oracle.COM 	extern int ascii_strncasecmp(const char *, const char *, size_t);
600*13093SRoger.Faulkner@Oracle.COM 
601*13093SRoger.Faulkner@Oracle.COM 	return (ascii_strncasecmp(s1, s2, n));
602*13093SRoger.Faulkner@Oracle.COM }
603*13093SRoger.Faulkner@Oracle.COM 
604*13093SRoger.Faulkner@Oracle.COM /*
6055891Sraf  * The following functions are cancellation points in libc.
6065891Sraf  * They are called from other functions in libc that we extract
6075891Sraf  * and use directly.  We don't do cancellation while we are in
6085891Sraf  * the dynamic linker, so we redefine these to call the primitive,
6095891Sraf  * non-cancellation interfaces.
6105891Sraf  */
6115891Sraf int
close(int fildes)6126812Sraf close(int fildes)
6135891Sraf {
6145891Sraf 	extern int __close(int);
6155891Sraf 
6165891Sraf 	return (__close(fildes));
6175891Sraf }
6185891Sraf 
6195891Sraf int
fcntl(int fildes,int cmd,...)6206812Sraf fcntl(int fildes, int cmd, ...)
6215891Sraf {
6225891Sraf 	extern int __fcntl(int, int, ...);
6235891Sraf 	intptr_t arg;
6245891Sraf 	va_list ap;
6255891Sraf 
6265891Sraf 	va_start(ap, cmd);
6275891Sraf 	arg = va_arg(ap, intptr_t);
6285891Sraf 	va_end(ap);
6295891Sraf 	return (__fcntl(fildes, cmd, arg));
6305891Sraf }
6315891Sraf 
6325891Sraf int
open(const char * path,int oflag,...)6336812Sraf open(const char *path, int oflag, ...)
6345891Sraf {
63511798SRoger.Faulkner@Sun.COM 	extern int __open(const char *, int, mode_t);
6365891Sraf 	mode_t mode;
6375891Sraf 	va_list ap;
6385891Sraf 
6395891Sraf 	va_start(ap, oflag);
6405891Sraf 	mode = va_arg(ap, mode_t);
6415891Sraf 	va_end(ap);
6425891Sraf 	return (__open(path, oflag, mode));
6435891Sraf }
6445891Sraf 
6455891Sraf int
openat(int fd,const char * path,int oflag,...)6466812Sraf openat(int fd, const char *path, int oflag, ...)
6475891Sraf {
64811798SRoger.Faulkner@Sun.COM 	extern int __openat(int, const char *, int, mode_t);
6495891Sraf 	mode_t mode;
6505891Sraf 	va_list ap;
6515891Sraf 
6525891Sraf 	va_start(ap, oflag);
6535891Sraf 	mode = va_arg(ap, mode_t);
6545891Sraf 	va_end(ap);
6555891Sraf 	return (__openat(fd, path, oflag, mode));
6565891Sraf }
6575891Sraf 
6585891Sraf ssize_t
read(int fd,void * buf,size_t size)6596812Sraf read(int fd, void *buf, size_t size)
6605891Sraf {
6615891Sraf 	extern ssize_t __read(int, void *, size_t);
6625891Sraf 	return (__read(fd, buf, size));
6635891Sraf }
6645891Sraf 
6655891Sraf ssize_t
write(int fd,const void * buf,size_t size)6666812Sraf write(int fd, const void *buf, size_t size)
6675891Sraf {
6685891Sraf 	extern ssize_t __write(int, const void *, size_t);
6695891Sraf 	return (__write(fd, buf, size));
6705891Sraf }
671