xref: /illumos-gate/usr/src/cmd/sgs/rtld/common/external.c (revision 2c2b5e89)
110a4fa49Srie /*
210a4fa49Srie  * CDDL HEADER START
310a4fa49Srie  *
410a4fa49Srie  * The contents of this file are subject to the terms of the
510a4fa49Srie  * Common Development and Distribution License (the "License").
610a4fa49Srie  * You may not use this file except in compliance with the License.
710a4fa49Srie  *
810a4fa49Srie  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
910a4fa49Srie  * or http://www.opensolaris.org/os/licensing.
1010a4fa49Srie  * See the License for the specific language governing permissions
1110a4fa49Srie  * and limitations under the License.
1210a4fa49Srie  *
1310a4fa49Srie  * When distributing Covered Code, include this CDDL HEADER in each
1410a4fa49Srie  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1510a4fa49Srie  * If applicable, add the following below this CDDL HEADER, with the
1610a4fa49Srie  * fields enclosed by brackets "[]" replaced with your own identifying
1710a4fa49Srie  * information: Portions Copyright [yyyy] [name of copyright owner]
1810a4fa49Srie  *
1910a4fa49Srie  * CDDL HEADER END
2010a4fa49Srie  */
2210a4fa49Srie /*
232020b2b6SRod Evans  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
242d08521bSGarrett D'Amore  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
25*2c2b5e89SPatrick Mooney  * Copyright (c) 2017, Joyent, Inc.
2610a4fa49Srie  */
2810a4fa49Srie /*
2910a4fa49Srie  * Implementation of all external interfaces between ld.so.1 and libc.
3010a4fa49Srie  *
3110a4fa49Srie  * This file started as a set of routines that provided synchronization and
3243d7826aSRod Evans  * locking operations using calls to libthread.  libthread has merged with libc
3343d7826aSRod Evans  * under the Unified Process Model (UPM), and things have gotten a lot simpler.
3443d7826aSRod Evans  * This file continues to establish and redirect various events within ld.so.1
3543d7826aSRod Evans  * to interfaces within libc.
3610a4fa49Srie  *
3710a4fa49Srie  * Until libc is loaded and relocated, any external interfaces are captured
3810a4fa49Srie  * locally.  Each link-map list maintains its own set of external vectors, as
3910a4fa49Srie  * each link-map list typically provides its own libc.  Although this per-link-
4010a4fa49Srie  * map list vectoring provides a degree of flexibility, there is a protocol
4110a4fa49Srie  * expected when calling various libc interfaces.
4210a4fa49Srie  *
4310a4fa49Srie  * i.	Any new alternative link-map list should call CI_THRINIT, and then call
4410a4fa49Srie  *	CI_TLS_MODADD to register any TLS for each object of that link-map list
4510a4fa49Srie  *	(this item is labeled i. as auditors can be the first objects loaded,
4610a4fa49Srie  *	and they exist on their own lik-map list).
4710a4fa49Srie  *
4810a4fa49Srie  * ii.	For the primary link-map list, CI_TLS_STATMOD must be called first to
4910a4fa49Srie  *	register any static TLS.  This routine is called regardless of there
5010a4fa49Srie  *	being any TLS, as this routine also establishes the link-map list as the
5110a4fa49Srie  *	primary list and fixes the association of uberdata).  CI_THRINIT should
5210a4fa49Srie  *	then be called.
5310a4fa49Srie  *
5410a4fa49Srie  * iii.	Any objects added to an existing link-map list (primary or alternative)
5510a4fa49Srie  *	should call CI_TLS_MODADD to register any additional TLS.
5610a4fa49Srie  *
5710a4fa49Srie  * These events are established by:
5810a4fa49Srie  *
5910a4fa49Srie  * i.	Typically, libc is loaded as part of the primary dependencies of any
6010a4fa49Srie  *	link-map list (since the Unified Process Model (UPM), libc can't be
6110a4fa49Srie  *	lazily loaded).  To minimize the possibility of loading and registering
6210a4fa49Srie  *	objects, and then tearing them down (because of a relocation error),
6310a4fa49Srie  *	external vectors are established as part of load_completion().  This
6410a4fa49Srie  *	routine is called on completion of any operation that can cause objects
6510a4fa49Srie  *	to be loaded.  This point of control insures the objects have been fully
6610a4fa49Srie  *	analyzed and relocated, and moved to their controlling link-map list.
6710a4fa49Srie  *	The external vectors are established prior to any .inits being fired.
6810a4fa49Srie  *
6910a4fa49Srie  * ii.	Calls to CI_THRINIT, and CI_TLS_MODADD also occur as part of
7010a4fa49Srie  *	load_completion().  CI_THRINIT is only called once for each link-map
7110a4fa49Srie  *	control list.
7210a4fa49Srie  *
7310a4fa49Srie  * iii.	Calls to CI_TLS_STATMOD, and CI_THRINIT occur for the primary link-map
7410a4fa49Srie  *	list in the final stages of setup().
7510a4fa49Srie  *
7610a4fa49Srie  * The interfaces provide by libc can be divided into two families.  The first
7710a4fa49Srie  * family consists of those interfaces that should be called from the link-map
7810a4fa49Srie  * list.  It's possible that these interfaces convey state concerning the
7910a4fa49Srie  * link-map list they are part of:
8010a4fa49Srie  *
8110a4fa49Srie  *	CI_ATEXIT
8210a4fa49Srie  *	CI TLS_MODADD
8310a4fa49Srie  *	CI_TLS_MODREM
8410a4fa49Srie  *	CI_TLS_STATMOD
8510a4fa49Srie  *	CI_THRINIT
8610a4fa49Srie  *
8710a4fa49Srie  * The second family are global in nature, that is, the link-map list from
8810a4fa49Srie  * which they are called provides no state information.  In fact, for
8910a4fa49Srie  * CI_BIND_GUARD, the calling link-map isn't even known.  The link-map can only
9010a4fa49Srie  * be deduced after ld.so.1's global lock has been obtained.  Therefore, the
9110a4fa49Srie  * following interfaces are also maintained as global:
9210a4fa49Srie  *
9310a4fa49Srie  *	CI_LCMESSAGES
9410a4fa49Srie  *	CI_BIND_GUARD
9510a4fa49Srie  *	CI_BIND_CLEAR
9610a4fa49Srie  *	CI_THR_SELF
9710a4fa49Srie  *
9810a4fa49Srie  * Note, it is possible that these global interfaces are obtained from an
9910a4fa49Srie  * alternative link-map list that gets torn down because of a processing
10010a4fa49Srie  * failure (unlikely, because the link-map list components must be analyzed
10110a4fa49Srie  * and relocated prior to load_completion(), but perhaps the tear down is still
10210a4fa49Srie  * a possibility).  Thus the global interfaces may have to be replaced.  Once
10310a4fa49Srie  * the interfaces have been obtained from the primary link-map, they can
10410a4fa49Srie  * remain fixed, as the primary link-map isn't going to go anywhere.
10510a4fa49Srie  *
10610a4fa49Srie  * The last wrinkle in the puzzle is what happens if an alternative link-map
10710a4fa49Srie  * is loaded with no libc dependency?  In this case, the alternative objects
10810a4fa49Srie  * can not call CI_THRINIT, can not be allowed to use TLS, and will not receive
10910a4fa49Srie  * any atexit processing.
11010a4fa49Srie  *
11110a4fa49Srie  * The history of these external interfaces is defined by their version:
11210a4fa49Srie  *
11310a4fa49Srie  * TI_VERSION == 1
11410a4fa49Srie  *	Under this model libthread provided rw_rwlock/rw_unlock, through which
11510a4fa49Srie  *	all rt_mutex_lock/rt_mutex_unlock calls were vectored.
11610a4fa49Srie  *	Under libc/libthread these interfaces provided _sigon/_sigoff (unlike
11710a4fa49Srie  *	lwp/libthread that provided signal blocking via bind_guard/bind_clear).
11810a4fa49Srie  *
11910a4fa49Srie  * TI_VERSION == 2
12010a4fa49Srie  *	Under this model only libthreads bind_guard/bind_clear and thr_self
12110a4fa49Srie  *	interfaces were used.  Both libthreads blocked signals under the
12210a4fa49Srie  *	bind_guard/bind_clear interfaces.   Lower level locking is derived
12310a4fa49Srie  *	from internally bound _lwp_ interfaces.  This removes recursive
12410a4fa49Srie  *	problems encountered when obtaining locking interfaces from libthread.
12510a4fa49Srie  *	The use of mutexes over reader/writer locks also enables the use of
12610a4fa49Srie  *	condition variables for controlling thread concurrency (allows access
12710a4fa49Srie  *	to objects only after their .init has completed).
12810a4fa49Srie  *
12910a4fa49Srie  * NOTE, the TI_VERSION indicated the ti_interface version number, where the
13010a4fa49Srie  * ti_interface was a large vector of functions passed to both libc (to override
13110a4fa49Srie  * the thread stub interfaces) and ld.so.1.  ld.so.1 used only a small subset of
13210a4fa49Srie  * these interfaces.
13310a4fa49Srie  *
13410a4fa49Srie  * CI_VERSION == 1
13510a4fa49Srie  *	Introduced with CI_VERSION & CI_ATEXIT
13610a4fa49Srie  *
13710a4fa49Srie  * CI_VERSION == 2 (Solaris 8 update 2).
13810a4fa49Srie  *	Added support for CI_LCMESSAGES
13910a4fa49Srie  *
14010a4fa49Srie  * CI_VERSION == 3 (Solaris 9).
14110a4fa49Srie  *	Added the following versions to the CI table:
14210a4fa49Srie  *
14510a4fa49Srie  *
14610a4fa49Srie  *	This version introduced the DT_SUNW_RTLDINFO structure as a mechanism
14710a4fa49Srie  *	to handshake with ld.so.1.
14810a4fa49Srie  *
14910a4fa49Srie  * CI_VERSION == 4 (Solaris 10).
15010a4fa49Srie  *	Added the CI_THRINIT handshake as part of the libc/libthread unified
15110a4fa49Srie  *	process model.  libc now initializes the current thread pointer from
15210a4fa49Srie  *	this interface (and no longer relies on the INITFIRST flag - which
15310a4fa49Srie  *	others have started to camp out on).
15410a4fa49Srie  *
1552a8d6ebaSRod Evans  * CI_VERSION == 5 (Solaris 11).
1562a8d6ebaSRod Evans  *	Use of "protected" references within libc, so that symbols are
1572a8d6ebaSRod Evans  *	pre-bound, and don't require ld.so.1 binding.  This implementation
1582a8d6ebaSRod Evans  *	protects libc's critical regions from being vectored to auditors.
1592a8d6ebaSRod Evans  *
1602a8d6ebaSRod Evans  * CI_VERSION == 6 (Solaris 11).
1612a8d6ebaSRod Evans  *	Added the CI_CRITICAL handshake, to allow "mem*" family to be reexposed
1622a8d6ebaSRod Evans  *	as "global", and thus be redirected to auxiliary filters.
1632a8d6ebaSRod Evans  *
16410a4fa49Srie  * Release summary:
16510a4fa49Srie  *
16610a4fa49Srie  *	Solaris 8	CI_ATEXIT via _ld_libc()
16710a4fa49Srie  *			TI_* via _ld_concurrency()
16810a4fa49Srie  *
16910a4fa49Srie  *	Solaris 9	CI_ATEXIT and CI_LCMESSAGES via _ld_libc()
17010a4fa49Srie  *			CI_* via RTLDINFO and _ld_libc()  - new libthread
17110a4fa49Srie  *			TI_* via _ld_concurrency()  - old libthread
17210a4fa49Srie  *
17310a4fa49Srie  *	Solaris 10	CI_ATEXIT and CI_LCMESSAGES via _ld_libc()
17410a4fa49Srie  *			CI_* via RTLDINFO and _ld_libc()  - new libthread
17510a4fa49Srie  */
177a574db85Sraf #include <sys/debug.h>
178a574db85Sraf #include <synch.h>
179a574db85Sraf #include <signal.h>
180a574db85Sraf #include <thread.h>
181a574db85Sraf #include <synch.h>
182a574db85Sraf #include <strings.h>
183a574db85Sraf #include <stdio.h>
184b599bd93SRobert Mustacchi #include <libintl.h>
185a574db85Sraf #include <debug.h>
186a574db85Sraf #include <libc_int.h>
187a574db85Sraf #include "_elf.h"
188a574db85Sraf #include "_rtld.h"
19010a4fa49Srie /*
19110a4fa49Srie  * This interface provides the unified process model communication between
19243d7826aSRod Evans  * ld.so.1 and libc.  This interface can be called a number of times:
19343d7826aSRod Evans  *
19443d7826aSRod Evans  *   -	Initially, this interface is called to process RTLDINFO.  This data
19543d7826aSRod Evans  *	structure is typically provided by libc, and contains the address of
19643d7826aSRod Evans  *	libc interfaces that must be called to initialize threads information.
19743d7826aSRod Evans  *
19843d7826aSRod Evans  *   -	_ld_libc(), this interface can also be called by libc at process
19943d7826aSRod Evans  *	initialization, after libc has been loaded and relocated, but before
20043d7826aSRod Evans  *	control has been passed to any user code (.init's or main()).  This
20143d7826aSRod Evans  *	call provides additional libc interface information that ld.so.1 must
20243d7826aSRod Evans  *	call during process execution.
20343d7826aSRod Evans  *
20443d7826aSRod Evans  *   -	_ld_libc() can also be called by libc during process execution to
2055e90af26SToomas Soome  *	re-establish interfaces such as the locale.
20610a4fa49Srie  */
20743d7826aSRod Evans static void
get_lcinterface(Rt_map * lmp,Lc_interface * funcs)20810a4fa49Srie get_lcinterface(Rt_map *lmp, Lc_interface *funcs)
20910a4fa49Srie {
21043d7826aSRod Evans 	int		threaded = 0, entry = 0, tag;
21110a4fa49Srie 	Lm_list		*lml;
21210a4fa49Srie 	Lc_desc		*lcp;
21443d7826aSRod Evans 	if ((lmp == NULL) || (funcs == NULL))
21510a4fa49Srie 		return;
21743d7826aSRod Evans 	/*
21843d7826aSRod Evans 	 * Once the process is active, ensure we grab a lock.
21943d7826aSRod Evans 	 */
22043d7826aSRod Evans 	if (rtld_flags & RT_FL_APPLIC)
22143d7826aSRod Evans 		entry = enter(0);
22243d7826aSRod Evans 
22310a4fa49Srie 	lml = LIST(lmp);
22410a4fa49Srie 	lcp = &lml->lm_lcs[0];
22610a4fa49Srie 	DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
22810a4fa49Srie 	for (tag = funcs->ci_tag; tag; tag = (++funcs)->ci_tag) {
22910a4fa49Srie 		char	*gptr;
23010a4fa49Srie 		char	*lptr = funcs->ci_un.ci_ptr;
23210a4fa49Srie 		DBG_CALL(Dbg_util_lcinterface(lmp, tag, lptr));
23410a4fa49Srie 		if (tag >= CI_MAX)
23510a4fa49Srie 			continue;
23710a4fa49Srie 		/*
23810a4fa49Srie 		 * Maintain all interfaces on a per-link-map basis.  Note, for
23910a4fa49Srie 		 * most interfaces, only the first interface is used for any
24010a4fa49Srie 		 * link-map list.  This prevents accidents with developers who
24110a4fa49Srie 		 * manage to load two different versions of libc.
24210a4fa49Srie 		 */
24310a4fa49Srie 		if ((lcp[tag].lc_lmp) &&
24410a4fa49Srie 		    (tag != CI_LCMESSAGES) && (tag != CI_VERSION)) {
24510a4fa49Srie 			DBG_CALL(Dbg_unused_lcinterface(lmp,
24610a4fa49Srie 			    lcp[tag].lc_lmp, tag));
24710a4fa49Srie 			continue;
24810a4fa49Srie 		}
25010a4fa49Srie 		lcp[tag].lc_un.lc_ptr = lptr;
25110a4fa49Srie 		lcp[tag].lc_lmp = lmp;
25310a4fa49Srie 		gptr = glcs[tag].lc_un.lc_ptr;
25510a4fa49Srie 		/*
25610a4fa49Srie 		 * Process any interfaces that must be maintained on a global
25710a4fa49Srie 		 * basis.
25810a4fa49Srie 		 */
25910a4fa49Srie 		switch (tag) {
26010a4fa49Srie 		case CI_ATEXIT:
26110a4fa49Srie 			break;
26310a4fa49Srie 		case CI_LCMESSAGES:
26410a4fa49Srie 			/*
26510a4fa49Srie 			 * At startup, ld.so.1 can establish a locale from one
26610a4fa49Srie 			 * of the locale family of environment variables (see
26710a4fa49Srie 			 * ld_str_env() and readenv_user()).  During process
26810a4fa49Srie 			 * execution the locale can also be changed by the user.
26910a4fa49Srie 			 * This interface is called from libc should the locale
27010a4fa49Srie 			 * be modified.  Presently, only one global locale is
27110a4fa49Srie 			 * maintained for all link-map lists, and only objects
27210a4fa49Srie 			 * on the primrary link-map may change this locale.
27310a4fa49Srie 			 */
27410a4fa49Srie 			if ((lml->lm_flags & LML_FLG_BASELM) &&
27543d7826aSRod Evans 			    ((gptr == NULL) || (strcmp(gptr, lptr) != 0))) {
27610a4fa49Srie 				/*
27710a4fa49Srie 				 * If we've obtained a message locale (typically
27810a4fa49Srie 				 * supplied via libc's setlocale()), then
27910a4fa49Srie 				 * register the locale for use in dgettext() so
28010a4fa49Srie 				 * as to reestablish the locale for ld.so.1's
28110a4fa49Srie 				 * messages.
28210a4fa49Srie 				 */
28310a4fa49Srie 				if (gptr) {
28410a4fa49Srie 					free((void *)gptr);
28510a4fa49Srie 					rtld_flags |= RT_FL_NEWLOCALE;
28610a4fa49Srie 				}
28710a4fa49Srie 				glcs[tag].lc_un.lc_ptr = strdup(lptr);
28910a4fa49Srie 				/*
29010a4fa49Srie 				 * Clear any cached messages.
29110a4fa49Srie 				 */
29243d7826aSRod Evans 				bzero(err_strs, sizeof (err_strs));
29343d7826aSRod Evans 				nosym_str = NULL;
29410a4fa49Srie 			}
29510a4fa49Srie 			break;
29710a4fa49Srie 		case CI_BIND_GUARD:
29810a4fa49Srie 		case CI_BIND_CLEAR:
29910a4fa49Srie 		case CI_THR_SELF:
3002a8d6ebaSRod Evans 		case CI_CRITICAL:
30110a4fa49Srie 			/*
30210a4fa49Srie 			 * If the global vector is unset, or this is the primary
30310a4fa49Srie 			 * link-map, set the global vector.
30410a4fa49Srie 			 */
30543d7826aSRod Evans 			if ((gptr == NULL) || (lml->lm_flags & LML_FLG_BASELM))
30610a4fa49Srie 				glcs[tag].lc_un.lc_ptr = lptr;
30810a4fa49Srie 			/* FALLTHROUGH */
31010a4fa49Srie 		case CI_TLS_MODADD:
31110a4fa49Srie 		case CI_TLS_MODREM:
31210a4fa49Srie 		case CI_TLS_STATMOD:
31310a4fa49Srie 		case CI_THRINIT:
31410a4fa49Srie 			threaded++;
31510a4fa49Srie 			break;
31710a4fa49Srie 		case CI_VERSION:
31810a4fa49Srie 			if ((rtld_flags2 & RT_FL2_RTLDSEEN) == 0) {
31957ef7aa9SRod Evans 				Aliste	idx;
32057ef7aa9SRod Evans 				Lm_list	*lml2;
32157ef7aa9SRod Evans 				int	version;
32243d7826aSRod Evans 
32310a4fa49Srie 				rtld_flags2 |= RT_FL2_RTLDSEEN;
3258cd45542Sraf 				version = funcs->ci_un.ci_val;
3268cd45542Sraf #if defined(CI_V_FIVE)
3278cd45542Sraf 				if (version >= CI_V_FIVE) {
3288cd45542Sraf 					thr_flg_nolock = THR_FLG_NOLOCK;
3298cd45542Sraf 					thr_flg_reenter = THR_FLG_REENTER;
3308cd45542Sraf 				}
3318cd45542Sraf #endif
33243d7826aSRod Evans 				if (version < CI_V_FOUR)
33310a4fa49Srie 					break;
33543d7826aSRod Evans 				rtld_flags2 |= RT_FL2_UNIFPROC;
33643d7826aSRod Evans 
33743d7826aSRod Evans 				/*
33843d7826aSRod Evans 				 * We might have seen an auditor which is not
33943d7826aSRod Evans 				 * dependent on libc.  Such an auditor's link
34043d7826aSRod Evans 				 * map list has LML_FLG_HOLDLOCK set.  This
34143d7826aSRod Evans 				 * lock needs to be dropped.  Refer to
34243d7826aSRod Evans 				 * audit_setup() in audit.c.
34343d7826aSRod Evans 				 */
34443d7826aSRod Evans 				if ((rtld_flags2 & RT_FL2_HASAUDIT) == 0)
34543d7826aSRod Evans 					break;
34643d7826aSRod Evans 
34743d7826aSRod Evans 				/*
34857ef7aa9SRod Evans 				 * Yes, we did.  Take care of them.
34943d7826aSRod Evans 				 */
35057ef7aa9SRod Evans 				for (APLIST_TRAVERSE(dynlm_list, idx, lml2)) {
35143d7826aSRod Evans 					Rt_map *map = (Rt_map *)lml2->lm_head;
35243d7826aSRod Evans 
35343d7826aSRod Evans 					if (FLAGS(map) & FLG_RT_AUDIT) {
35443d7826aSRod Evans 						lml2->lm_flags &=
35543d7826aSRod Evans 						    ~LML_FLG_HOLDLOCK;
35610a4fa49Srie 					}
35710a4fa49Srie 				}
35810a4fa49Srie 			}
35910a4fa49Srie 			break;
36110a4fa49Srie 		default:
36210a4fa49Srie 			break;
36310a4fa49Srie 		}
36410a4fa49Srie 	}
36643d7826aSRod Evans 	if (threaded) {
36743d7826aSRod Evans 		/*
36843d7826aSRod Evans 		 * If a version of libc gives us only a subset of the TLS
36943d7826aSRod Evans 		 * interfaces, it's confused and we discard the whole lot.
37043d7826aSRod Evans 		 */
37167d74cc3SToomas Soome 		if (((lcp[CI_TLS_MODADD].lc_un.lc_func != NULL) &&
37267d74cc3SToomas Soome 		    (lcp[CI_TLS_MODREM].lc_un.lc_func != NULL) &&
37367d74cc3SToomas Soome 		    (lcp[CI_TLS_STATMOD].lc_un.lc_func != NULL)) == 0) {
37443d7826aSRod Evans 			lcp[CI_TLS_MODADD].lc_un.lc_func = NULL;
37543d7826aSRod Evans 			lcp[CI_TLS_MODREM].lc_un.lc_func = NULL;
37643d7826aSRod Evans 			lcp[CI_TLS_STATMOD].lc_un.lc_func = NULL;
37743d7826aSRod Evans 		}
37943d7826aSRod Evans 		/*
38043d7826aSRod Evans 		 * Indicate that we're now thread capable.
38143d7826aSRod Evans 		 */
38243d7826aSRod Evans 		if ((lml->lm_flags & LML_FLG_RTLDLM) == 0)
38343d7826aSRod Evans 			rtld_flags |= RT_FL_THREADS;
38410a4fa49Srie 	}
38643d7826aSRod Evans 	if (entry)
38743d7826aSRod Evans 		leave(lml, 0);
38810a4fa49Srie }
39010a4fa49Srie /*
39110a4fa49Srie  * At this point we know we have a set of objects that have been fully analyzed
39210a4fa49Srie  * and relocated.  Prior to the next major step of running .init sections (ie.
39310a4fa49Srie  * running user code), retrieve any RTLDINFO interfaces.
39410a4fa49Srie  */
39510a4fa49Srie int
rt_get_extern(Lm_list * lml,Rt_map * lmp)39610a4fa49Srie rt_get_extern(Lm_list *lml, Rt_map *lmp)
39710a4fa49Srie {
39810a4fa49Srie 	if (lml->lm_rti) {
399cce0e03bSab 		Aliste		idx;
40010a4fa49Srie 		Rti_desc	*rti;
402cce0e03bSab 		for (ALIST_TRAVERSE(lml->lm_rti, idx, rti))
40310a4fa49Srie 			get_lcinterface(rti->rti_lmp, rti->rti_info);
40510a4fa49Srie 		free(lml->lm_rti);
40610a4fa49Srie 		lml->lm_rti = 0;
40710a4fa49Srie 	}
40910a4fa49Srie 	/*
41010a4fa49Srie 	 * Perform some sanity checks.  If we have TLS requirements we better
41110a4fa49Srie 	 * have the associated external interfaces.
41210a4fa49Srie 	 */
41343d7826aSRod Evans 	if (lml->lm_tls &&
41443d7826aSRod Evans 	    (lml->lm_lcs[CI_TLS_STATMOD].lc_un.lc_func == NULL)) {
415d326b23bSrie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_TLS_NOSUPPORT),
41610a4fa49Srie 		    NAME(lmp));
41710a4fa49Srie 		return (0);
41810a4fa49Srie 	}
41910a4fa49Srie 	return (1);
42010a4fa49Srie }
42243d7826aSRod Evans /*
42343d7826aSRod Evans  * Provide an interface for libc to communicate additional interface
42443d7826aSRod Evans  * information.
42543d7826aSRod Evans  */
42643d7826aSRod Evans void
_ld_libc(void * ptr)42743d7826aSRod Evans _ld_libc(void *ptr)
42843d7826aSRod Evans {
42943d7826aSRod Evans 	get_lcinterface(_caller(caller(), CL_EXECDEF), (Lc_interface *)ptr);
43043d7826aSRod Evans }
43143d7826aSRod Evans 
43210a4fa49Srie static int	bindmask = 0;
43410a4fa49Srie int
rt_bind_guard(int flags)4358cd45542Sraf rt_bind_guard(int flags)
43610a4fa49Srie {
43710a4fa49Srie 	int	(*fptr)(int);
4388cd45542Sraf 	int	bindflag;
44010a4fa49Srie 	if ((fptr = glcs[CI_BIND_GUARD].lc_un.lc_func) != NULL) {
4418cd45542Sraf 		return ((*fptr)(flags));
44210a4fa49Srie 	} else {
4438cd45542Sraf 		bindflag = (flags & THR_FLG_RTLD);
44410a4fa49Srie 		if ((bindflag & bindmask) == 0) {
44510a4fa49Srie 			bindmask |= bindflag;
44610a4fa49Srie 			return (1);
44710a4fa49Srie 		}
44810a4fa49Srie 		return (0);
44910a4fa49Srie 	}
45010a4fa49Srie }
45210a4fa49Srie int
rt_bind_clear(int flags)4538cd45542Sraf rt_bind_clear(int flags)
45410a4fa49Srie {
45510a4fa49Srie 	int	(*fptr)(int);
4568cd45542Sraf 	int	bindflag;
45810a4fa49Srie 	if ((fptr = glcs[CI_BIND_CLEAR].lc_un.lc_func) != NULL) {
4598cd45542Sraf 		return ((*fptr)(flags));
46010a4fa49Srie 	} else {
4618cd45542Sraf 		bindflag = (flags & THR_FLG_RTLD);
46210a4fa49Srie 		if (bindflag == 0)
46310a4fa49Srie 			return (bindmask);
46410a4fa49Srie 		else {
46510a4fa49Srie 			bindmask &= ~bindflag;
46610a4fa49Srie 			return (0);
46710a4fa49Srie 		}
46810a4fa49Srie 	}
46910a4fa49Srie }
47110a4fa49Srie /*
47210a4fa49Srie  * Make sure threads have been initialized.  This interface is called once for
47310a4fa49Srie  * each link-map list.
47410a4fa49Srie  */
47510a4fa49Srie void
rt_thr_init(Lm_list * lml)47610a4fa49Srie rt_thr_init(Lm_list *lml)
47710a4fa49Srie {
4785e90af26SToomas Soome 	int	(*fptr)(void);
4805e90af26SToomas Soome 	if ((fptr = lml->lm_lcs[CI_THRINIT].lc_un.lc_func) != NULL) {
48143d7826aSRod Evans 		lml->lm_lcs[CI_THRINIT].lc_un.lc_func = NULL;
4822020b2b6SRod Evans 
4832020b2b6SRod Evans 		leave(lml, thr_flg_reenter);
4845e90af26SToomas Soome 		(void) (*fptr)();
4858cd45542Sraf 		(void) enter(thr_flg_reenter);
486dde769a2SRod Evans 
487dde769a2SRod Evans 		/*
488dde769a2SRod Evans 		 * If this is an alternative link-map list, and this is the
489dde769a2SRod Evans 		 * first call to initialize threads, don't let the destination
490dde769a2SRod Evans 		 * libc be deleted.  It is possible that an auditors complete
491dde769a2SRod Evans 		 * initialization fails, but there is presently no main link-map
492dde769a2SRod Evans 		 * list.  As this libc has established the thread pointer, don't
493dde769a2SRod Evans 		 * delete this libc, otherwise the initialization of libc on the
494dde769a2SRod Evans 		 * main link-map can be compromised during its threads
495dde769a2SRod Evans 		 * initialization.
496dde769a2SRod Evans 		 */
497dde769a2SRod Evans 		if (((lml->lm_flags & LML_FLG_BASELM) == 0) &&
498dde769a2SRod Evans 		    ((rtld_flags2 & RT_FL2_PLMSETUP) == 0))
499dde769a2SRod Evans 			MODE(lml->lm_lcs[CI_THRINIT].lc_lmp) |= RTLD_NODELETE;
50010a4fa49Srie 	}
50110a4fa49Srie }
50310a4fa49Srie thread_t
rt_thr_self()50410a4fa49Srie rt_thr_self()
50510a4fa49Srie {
50610a4fa49Srie 	thread_t	(*fptr)(void);
50810a4fa49Srie 	if ((fptr = (thread_t (*)())glcs[CI_THR_SELF].lc_un.lc_func) != NULL)
50910a4fa49Srie 		return ((*fptr)());
51110a4fa49Srie 	return (1);
51210a4fa49Srie }
51410a4fa49Srie int
rt_mutex_lock(Rt_lock * mp)5152a8d6ebaSRod Evans rt_mutex_lock(Rt_lock *mp)
51610a4fa49Srie {
51710a4fa49Srie 	return (_lwp_mutex_lock((lwp_mutex_t *)mp));
51810a4fa49Srie }
52010a4fa49Srie int
rt_mutex_unlock(Rt_lock * mp)5212a8d6ebaSRod Evans rt_mutex_unlock(Rt_lock *mp)
52210a4fa49Srie {
52310a4fa49Srie 	return (_lwp_mutex_unlock((lwp_mutex_t *)mp));
52410a4fa49Srie }
5262a8d6ebaSRod Evans /*
5272a8d6ebaSRod Evans  * Test whether we're in a libc critical region.  Certain function references,
5282a8d6ebaSRod Evans  * like the "mem*" family, might require binding.  Although these functions can
5292a8d6ebaSRod Evans  * safely bind to auxiliary filtees, they should not be captured by auditors.
5302a8d6ebaSRod Evans  */
5312a8d6ebaSRod Evans int
rt_critical()5322a8d6ebaSRod Evans rt_critical()
5332a8d6ebaSRod Evans {
5342a8d6ebaSRod Evans 	int	(*fptr)(void);
5352a8d6ebaSRod Evans 
5362a8d6ebaSRod Evans 	if ((fptr = glcs[CI_CRITICAL].lc_un.lc_func) != NULL)
5372a8d6ebaSRod Evans 		return ((*fptr)());
5382a8d6ebaSRod Evans 
5392a8d6ebaSRod Evans 	return (0);
5402a8d6ebaSRod Evans }
5412a8d6ebaSRod Evans 
54210a4fa49Srie /*
54310a4fa49Srie  * Mutex interfaces to resolve references from any objects extracted from
54410a4fa49Srie  * libc_pic.a.  Note, as ld.so.1 is essentially single threaded these can be
54510a4fa49Srie  * noops.
54610a4fa49Srie  */
5477257d1b4Sraf #pragma weak lmutex_lock = mutex_lock
54810a4fa49Srie /* ARGSUSED */
54910a4fa49Srie int
mutex_lock(mutex_t * mp)5507257d1b4Sraf mutex_lock(mutex_t *mp)
55110a4fa49Srie {
55210a4fa49Srie 	return (0);
55310a4fa49Srie }
5557257d1b4Sraf #pragma weak lmutex_unlock = mutex_unlock
55610a4fa49Srie /* ARGSUSED */
55710a4fa49Srie int
mutex_unlock(mutex_t * mp)5587257d1b4Sraf mutex_unlock(mutex_t *mp)
55910a4fa49Srie {
56010a4fa49Srie 	return (0);
56110a4fa49Srie }
563494a4c51Sraf /* ARGSUSED */
564494a4c51Sraf int
mutex_init(mutex_t * mp,int type,void * arg)5657257d1b4Sraf mutex_init(mutex_t *mp, int type, void *arg)
566494a4c51Sraf {
567494a4c51Sraf 	return (0);
568494a4c51Sraf }
570494a4c51Sraf /* ARGSUSED */
571494a4c51Sraf int
mutex_destroy(mutex_t * mp)5727257d1b4Sraf mutex_destroy(mutex_t *mp)
573494a4c51Sraf {
574494a4c51Sraf 	return (0);
575494a4c51Sraf }
57710a4fa49Srie /*
57810a4fa49Srie  * This is needed to satisfy sysconf() (case _SC_THREAD_STACK_MIN)
57910a4fa49Srie  */
58010a4fa49Srie size_t
thr_min_stack()5817257d1b4Sraf thr_min_stack()
58210a4fa49Srie {
5832020b2b6SRod Evans 	return (sizeof (uintptr_t) * 1024);
58410a4fa49Srie }
58623a1cceaSRoger A. Faulkner /*
58723a1cceaSRoger A. Faulkner  * Local str[n]casecmp() interfaces for the dynamic linker,
58823a1cceaSRoger A. Faulkner  * to avoid problems when linking with libc_pic.a
58923a1cceaSRoger A. Faulkner  */
59023a1cceaSRoger A. Faulkner int
strcasecmp(const char * s1,const char * s2)59123a1cceaSRoger A. Faulkner strcasecmp(const char *s1, const char *s2)
59223a1cceaSRoger A. Faulkner {
59323a1cceaSRoger A. Faulkner 	extern int ascii_strcasecmp(const char *, const char *);
59423a1cceaSRoger A. Faulkner 
59523a1cceaSRoger A. Faulkner 	return (ascii_strcasecmp(s1, s2));
59623a1cceaSRoger A. Faulkner }
59723a1cceaSRoger A. Faulkner 
59823a1cceaSRoger A. Faulkner int
strncasecmp(const char * s1,const char * s2,size_t n)59923a1cceaSRoger A. Faulkner strncasecmp(const char *s1, const char *s2, size_t n)
60023a1cceaSRoger A. Faulkner {
60123a1cceaSRoger A. Faulkner 	extern int ascii_strncasecmp(const char *, const char *, size_t);
60223a1cceaSRoger A. Faulkner 
60323a1cceaSRoger A. Faulkner 	return (ascii_strncasecmp(s1, s2, n));
60423a1cceaSRoger A. Faulkner }
60523a1cceaSRoger A. Faulkner 
606a574db85Sraf /*
607a574db85Sraf  * The following functions are cancellation points in libc.
608a574db85Sraf  * They are called from other functions in libc that we extract
609a574db85Sraf  * and use directly.  We don't do cancellation while we are in
610a574db85Sraf  * the dynamic linker, so we redefine these to call the primitive,
611a574db85Sraf  * non-cancellation interfaces.
612a574db85Sraf  */
613a574db85Sraf int
close(int fildes)6147257d1b4Sraf close(int fildes)
615a574db85Sraf {
616a574db85Sraf 	extern int __close(int);
618a574db85Sraf 	return (__close(fildes));
619a574db85Sraf }
621a574db85Sraf int
fcntl(int fildes,int cmd,...)6227257d1b4Sraf fcntl(int fildes, int cmd, ...)
623a574db85Sraf {
624a574db85Sraf 	extern int __fcntl(int, int, ...);
625a574db85Sraf 	intptr_t arg;
626a574db85Sraf 	va_list ap;
628a574db85Sraf 	va_start(ap, cmd);
629a574db85Sraf 	arg = va_arg(ap, intptr_t);
630a574db85Sraf 	va_end(ap);
631a574db85Sraf 	return (__fcntl(fildes, cmd, arg));
632a574db85Sraf }
634a574db85Sraf int
open(const char * path,int oflag,...)6357257d1b4Sraf open(const char *path, int oflag, ...)
636a574db85Sraf {
6378fd04b83SRoger A. Faulkner 	extern int __open(const char *, int, mode_t);
638a574db85Sraf 	mode_t mode;
639a574db85Sraf 	va_list ap;
641a574db85Sraf 	va_start(ap, oflag);
642a574db85Sraf 	mode = va_arg(ap, mode_t);
643a574db85Sraf 	va_end(ap);
644a574db85Sraf 	return (__open(path, oflag, mode));
645a574db85Sraf }
647a574db85Sraf int
openat(int fd,const char * path,int oflag,...)6487257d1b4Sraf openat(int fd, const char *path, int oflag, ...)
649a574db85Sraf {
6508fd04b83SRoger A. Faulkner 	extern int __openat(int, const char *, int, mode_t);
651a574db85Sraf 	mode_t mode;
652a574db85Sraf 	va_list ap;
654a574db85Sraf 	va_start(ap, oflag);
655a574db85Sraf 	mode = va_arg(ap, mode_t);
656a574db85Sraf 	va_end(ap);
657a574db85Sraf 	return (__openat(fd, path, oflag, mode));
658a574db85Sraf }
660a574db85Sraf ssize_t
read(int fd,void * buf,size_t size)6617257d1b4Sraf read(int fd, void *buf, size_t size)
662a574db85Sraf {
663a574db85Sraf 	extern ssize_t __read(int, void *, size_t);
664a574db85Sraf 	return (__read(fd, buf, size));
665a574db85Sraf }
667a574db85Sraf ssize_t
write(int fd,const void * buf,size_t size)6687257d1b4Sraf write(int fd, const void *buf, size_t size)
669a574db85Sraf {
670a574db85Sraf 	extern ssize_t __write(int, const void *, size_t);
671a574db85Sraf 	return (__write(fd, buf, size));
672a574db85Sraf }
6732d08521bSGarrett D'Amore 
6742d08521bSGarrett D'Amore /*
6752d08521bSGarrett D'Amore  * ASCII versions of ctype character classification functions.  This avoids
6762d08521bSGarrett D'Amore  * pulling in the entire locale framework that is in libc.
6772d08521bSGarrett D'Amore  */
6782d08521bSGarrett D'Amore 
6792d08521bSGarrett D'Amore int
isdigit(int c)6802d08521bSGarrett D'Amore isdigit(int c)
6812d08521bSGarrett D'Amore {
6822d08521bSGarrett D'Amore 	return ((c >= '0' && c <= '9') ? 1 : 0);
6832d08521bSGarrett D'Amore }
6842d08521bSGarrett D'Amore 
6852d08521bSGarrett D'Amore int
isupper(int c)6862d08521bSGarrett D'Amore isupper(int c)
6872d08521bSGarrett D'Amore {
6882d08521bSGarrett D'Amore 	return ((c >= 'A' && c <= 'Z') ? 1 : 0);
6892d08521bSGarrett D'Amore }
6902d08521bSGarrett D'Amore 
6912d08521bSGarrett D'Amore int
islower(int c)6922d08521bSGarrett D'Amore islower(int c)
6932d08521bSGarrett D'Amore {
6942d08521bSGarrett D'Amore 	return ((c >= 'a' && c <= 'z') ? 1 : 0);
6952d08521bSGarrett D'Amore }
6962d08521bSGarrett D'Amore 
6972d08521bSGarrett D'Amore int
isspace(int c)6982d08521bSGarrett D'Amore isspace(int c)
6992d08521bSGarrett D'Amore {
7002d08521bSGarrett D'Amore 	return (((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n') ||
7012d08521bSGarrett D'Amore 	    (c == '\v') || (c == '\f')) ? 1 : 0);
7022d08521bSGarrett D'Amore }
7032d08521bSGarrett D'Amore 
7042d08521bSGarrett D'Amore int
isxdigit(int c)7052d08521bSGarrett D'Amore isxdigit(int c)
7062d08521bSGarrett D'Amore {
7072d08521bSGarrett D'Amore 	return ((isdigit(c) || (c >= 'A' && c <= 'F') ||
7082d08521bSGarrett D'Amore 	    (c >= 'a' && c <= 'f')) ? 1 : 0);
7092d08521bSGarrett D'Amore }
7102d08521bSGarrett D'Amore 
7112d08521bSGarrett D'Amore int
isalpha(int c)7122d08521bSGarrett D'Amore isalpha(int c)
7132d08521bSGarrett D'Amore {
7142d08521bSGarrett D'Amore 	return ((isupper(c) || islower(c)) ? 1 : 0);
7152d08521bSGarrett D'Amore }
7162d08521bSGarrett D'Amore 
7172d08521bSGarrett D'Amore int
isalnum(int c)7182d08521bSGarrett D'Amore isalnum(int c)
7192d08521bSGarrett D'Amore {
7202d08521bSGarrett D'Amore 	return ((isalpha(c) || isdigit(c)) ? 1 : 0);
7212d08521bSGarrett D'Amore }
722b599bd93SRobert Mustacchi 
7232428aad8SPatrick Mooney #if defined(__i386) || defined(__amd64)
7242428aad8SPatrick Mooney /*
725*2c2b5e89SPatrick Mooney  * Instead of utilizing the comm page for clock_gettime and gettimeofday, rtld
726*2c2b5e89SPatrick Mooney  * uses the raw syscall instead.  Doing so decreases the surface of symbols
727*2c2b5e89SPatrick Mooney  * needed from libc for a modest performance cost.
7282428aad8SPatrick Mooney  */
7292428aad8SPatrick Mooney extern int __clock_gettime_sys(clockid_t, struct timespec *);
7302428aad8SPatrick Mooney 
7312428aad8SPatrick Mooney int
__clock_gettime(clockid_t clock_id,struct timespec * tp)7322428aad8SPatrick Mooney __clock_gettime(clockid_t clock_id, struct timespec *tp)
7332428aad8SPatrick Mooney {
7342428aad8SPatrick Mooney 	return (__clock_gettime_sys(clock_id, tp));
7352428aad8SPatrick Mooney }
736*2c2b5e89SPatrick Mooney 
737*2c2b5e89SPatrick Mooney int
gettimeofday(struct timeval * tv,void * tz)738*2c2b5e89SPatrick Mooney gettimeofday(struct timeval *tv, void *tz)
739*2c2b5e89SPatrick Mooney {
740*2c2b5e89SPatrick Mooney 	if (tv != NULL) {
741*2c2b5e89SPatrick Mooney 		/*
742*2c2b5e89SPatrick Mooney 		 * Perform the same logic as the libc gettimeofday() when it
743*2c2b5e89SPatrick Mooney 		 * lacks comm page support: Make the clock_gettime syscall and
744*2c2b5e89SPatrick Mooney 		 * divide out the tv_usec field as required.
745*2c2b5e89SPatrick Mooney 		 */
746*2c2b5e89SPatrick Mooney 		__clock_gettime_sys(CLOCK_REALTIME, (timespec_t *)tv);
747*2c2b5e89SPatrick Mooney 		tv->tv_usec /= 1000;
748*2c2b5e89SPatrick Mooney 	}
749*2c2b5e89SPatrick Mooney 
750*2c2b5e89SPatrick Mooney 	return (0);
751*2c2b5e89SPatrick Mooney }
7522428aad8SPatrick Mooney #endif /* defined(__i386) || defined(__amd64) */
7532428aad8SPatrick Mooney 
754b599bd93SRobert Mustacchi /*
755b599bd93SRobert Mustacchi  * In a similar vein to the is* functions above, we also have to define our own
756b599bd93SRobert Mustacchi  * version of strerror, as it is implemented in terms of the locale aware
757b599bd93SRobert Mustacchi  * strerror_l, and we'd rather not have the full set of libc symbols used here.
758b599bd93SRobert Mustacchi  */
759b599bd93SRobert Mustacchi extern const char _sys_errs[];
760b599bd93SRobert Mustacchi extern const int _sys_index[];
761b599bd93SRobert Mustacchi extern int _sys_num_err;
762b599bd93SRobert Mustacchi 
763b599bd93SRobert Mustacchi char *
strerror(int errnum)764b599bd93SRobert Mustacchi strerror(int errnum)
765b599bd93SRobert Mustacchi {
766b599bd93SRobert Mustacchi 	if (errnum < _sys_num_err && errnum >= 0) {
767b599bd93SRobert Mustacchi 		return (dgettext("SUNW_OST_OSLIB",
768b599bd93SRobert Mustacchi 		    (char *)&_sys_errs[_sys_index[errnum]]));
769b599bd93SRobert Mustacchi 	}
770b599bd93SRobert Mustacchi 
771b599bd93SRobert Mustacchi 	errno = EINVAL;
772b599bd93SRobert Mustacchi 	return (dgettext("SUNW_OST_OSLIB", "Unknown error"));
773b599bd93SRobert Mustacchi }