xref: /titanic_41/usr/src/cmd/sgs/rtld/common/external.c (revision c0c79a3f09914f35651895ffc111883455b7f62d)
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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Implementation of all external interfaces between ld.so.1 and libc.
30  *
31  * This file started as a set of routines that provided synchronization and
32  * locking operations using calls to libthread.  libthread has merged with libc,
33  * and things have gotten a little simpler.  This file continues to establish
34  * and redirect various events within ld.so.1 to interfaces within libc.
35  *
36  * Until libc is loaded and relocated, any external interfaces are captured
37  * locally.  Each link-map list maintains its own set of external vectors, as
38  * each link-map list typically provides its own libc.  Although this per-link-
39  * map list vectoring provides a degree of flexibility, there is a protocol
40  * expected when calling various libc interfaces.
41  *
42  * i.	Any new alternative link-map list should call CI_THRINIT, and then call
43  *	CI_TLS_MODADD to register any TLS for each object of that link-map list
44  *	(this item is labeled i. as auditors can be the first objects loaded,
45  *	and they exist on their own lik-map list).
46  *
47  * ii.	For the primary link-map list, CI_TLS_STATMOD must be called first to
48  *	register any static TLS.  This routine is called regardless of there
49  *	being any TLS, as this routine also establishes the link-map list as the
50  *	primary list and fixes the association of uberdata).  CI_THRINIT should
51  *	then be called.
52  *
53  * iii.	Any objects added to an existing link-map list (primary or alternative)
54  *	should call CI_TLS_MODADD to register any additional TLS.
55  *
56  * These events are established by:
57  *
58  * i.	Typically, libc is loaded as part of the primary dependencies of any
59  *	link-map list (since the Unified Process Model (UPM), libc can't be
60  *	lazily loaded).  To minimize the possibility of loading and registering
61  *	objects, and then tearing them down (because of a relocation error),
62  *	external vectors are established as part of load_completion().  This
63  *	routine is called on completion of any operation that can cause objects
64  *	to be loaded.  This point of control insures the objects have been fully
65  *	analyzed and relocated, and moved to their controlling link-map list.
66  *	The external vectors are established prior to any .inits being fired.
67  *
68  * ii.	Calls to CI_THRINIT, and CI_TLS_MODADD also occur as part of
69  *	load_completion().  CI_THRINIT is only called once for each link-map
70  *	control list.
71  *
72  * iii.	Calls to CI_TLS_STATMOD, and CI_THRINIT occur for the primary link-map
73  *	list in the final stages of setup().
74  *
75  * The interfaces provide by libc can be divided into two families.  The first
76  * family consists of those interfaces that should be called from the link-map
77  * list.  It's possible that these interfaces convey state concerning the
78  * link-map list they are part of:
79  *
80  *	CI_ATEXIT
81  *	CI TLS_MODADD
82  *	CI_TLS_MODREM
83  *	CI_TLS_STATMOD
84  *	CI_THRINIT
85  *
86  * The second family are global in nature, that is, the link-map list from
87  * which they are called provides no state information.  In fact, for
88  * CI_BIND_GUARD, the calling link-map isn't even known.  The link-map can only
89  * be deduced after ld.so.1's global lock has been obtained.  Therefore, the
90  * following interfaces are also maintained as global:
91  *
92  *	CI_LCMESSAGES
93  *	CI_BIND_GUARD
94  *	CI_BIND_CLEAR
95  *	CI_THR_SELF
96  *
97  * Note, it is possible that these global interfaces are obtained from an
98  * alternative link-map list that gets torn down because of a processing
99  * failure (unlikely, because the link-map list components must be analyzed
100  * and relocated prior to load_completion(), but perhaps the tear down is still
101  * a possibility).  Thus the global interfaces may have to be replaced.  Once
102  * the interfaces have been obtained from the primary link-map, they can
103  * remain fixed, as the primary link-map isn't going to go anywhere.
104  *
105  * The last wrinkle in the puzzle is what happens if an alternative link-map
106  * is loaded with no libc dependency?  In this case, the alternative objects
107  * can not call CI_THRINIT, can not be allowed to use TLS, and will not receive
108  * any atexit processing.
109  *
110  * The history of these external interfaces is defined by their version:
111  *
112  * TI_VERSION == 1
113  *	Under this model libthread provided rw_rwlock/rw_unlock, through which
114  *	all rt_mutex_lock/rt_mutex_unlock calls were vectored.
115  *	Under libc/libthread these interfaces provided _sigon/_sigoff (unlike
116  *	lwp/libthread that provided signal blocking via bind_guard/bind_clear).
117  *
118  * TI_VERSION == 2
119  *	Under this model only libthreads bind_guard/bind_clear and thr_self
120  *	interfaces were used.  Both libthreads blocked signals under the
121  *	bind_guard/bind_clear interfaces.   Lower level locking is derived
122  *	from internally bound _lwp_ interfaces.  This removes recursive
123  *	problems encountered when obtaining locking interfaces from libthread.
124  *	The use of mutexes over reader/writer locks also enables the use of
125  *	condition variables for controlling thread concurrency (allows access
126  *	to objects only after their .init has completed).
127  *
128  * NOTE, the TI_VERSION indicated the ti_interface version number, where the
129  * ti_interface was a large vector of functions passed to both libc (to override
130  * the thread stub interfaces) and ld.so.1.  ld.so.1 used only a small subset of
131  * these interfaces.
132  *
133  * CI_VERSION == 1
134  *	Introduced with CI_VERSION & CI_ATEXIT
135  *
136  * CI_VERSION == 2 (Solaris 8 update 2).
137  *	Added support for CI_LCMESSAGES
138  *
139  * CI_VERSION == 3 (Solaris 9).
140  *	Added the following versions to the CI table:
141  *
142  *		CI_BIND_GUARD, CI_BIND_CLEAR, CI_THR_SELF
143  *		CI_TLS_MODADD, CI_TLS_MOD_REMOVE, CI_TLS_STATMOD
144  *
145  *	This version introduced the DT_SUNW_RTLDINFO structure as a mechanism
146  *	to handshake with ld.so.1.
147  *
148  * CI_VERSION == 4 (Solaris 10).
149  *	Added the CI_THRINIT handshake as part of the libc/libthread unified
150  *	process model.  libc now initializes the current thread pointer from
151  *	this interface (and no longer relies on the INITFIRST flag - which
152  *	others have started to camp out on).
153  *
154  * Release summary:
155  *
156  *	Solaris 8	CI_ATEXIT via _ld_libc()
157  *			TI_* via _ld_concurrency()
158  *
159  *	Solaris 9	CI_ATEXIT and CI_LCMESSAGES via _ld_libc()
160  *			CI_* via RTLDINFO and _ld_libc()  - new libthread
161  *			TI_* via _ld_concurrency()  - old libthread
162  *
163  *	Solaris 10	CI_ATEXIT and CI_LCMESSAGES via _ld_libc()
164  *			CI_* via RTLDINFO and _ld_libc()  - new libthread
165  */
166 #include	"_synonyms.h"
167 
168 #include	<sys/debug.h>
169 #include	<synch.h>
170 #include	<signal.h>
171 #include	<thread.h>
172 #include	<synch.h>
173 #include	<strings.h>
174 #include	<stdio.h>
175 #include	<debug.h>
176 #include	<libc_int.h>
177 #include	"_elf.h"
178 #include	"_rtld.h"
179 
180 /*
181  * This interface provides the unified process model communication between
182  * ld.so.1 and libc.  This interface is supplied through RTLDINFO.
183  */
184 void
185 get_lcinterface(Rt_map *lmp, Lc_interface *funcs)
186 {
187 	int		tag, threaded = 0;
188 	Lm_list		*lml;
189 	Lc_desc		*lcp;
190 
191 	if ((lmp == 0) || (funcs == 0))
192 		return;
193 
194 	lml = LIST(lmp);
195 	lcp = &lml->lm_lcs[0];
196 
197 	DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
198 
199 	for (tag = funcs->ci_tag; tag; tag = (++funcs)->ci_tag) {
200 		char	*gptr;
201 		char	*lptr = funcs->ci_un.ci_ptr;
202 
203 		DBG_CALL(Dbg_util_lcinterface(lmp, tag, lptr));
204 
205 		if (tag >= CI_MAX)
206 			continue;
207 
208 		/*
209 		 * Maintain all interfaces on a per-link-map basis.  Note, for
210 		 * most interfaces, only the first interface is used for any
211 		 * link-map list.  This prevents accidents with developers who
212 		 * manage to load two different versions of libc.
213 		 */
214 		if ((lcp[tag].lc_lmp) &&
215 		    (tag != CI_LCMESSAGES) && (tag != CI_VERSION)) {
216 			DBG_CALL(Dbg_unused_lcinterface(lmp,
217 			    lcp[tag].lc_lmp, tag));
218 			continue;
219 		}
220 
221 		lcp[tag].lc_un.lc_ptr = lptr;
222 		lcp[tag].lc_lmp = lmp;
223 
224 		gptr = glcs[tag].lc_un.lc_ptr;
225 
226 		/*
227 		 * Process any interfaces that must be maintained on a global
228 		 * basis.
229 		 */
230 		switch (tag) {
231 		case CI_ATEXIT:
232 			break;
233 
234 		case CI_LCMESSAGES:
235 			/*
236 			 * At startup, ld.so.1 can establish a locale from one
237 			 * of the locale family of environment variables (see
238 			 * ld_str_env() and readenv_user()).  During process
239 			 * execution the locale can also be changed by the user.
240 			 * This interface is called from libc should the locale
241 			 * be modified.  Presently, only one global locale is
242 			 * maintained for all link-map lists, and only objects
243 			 * on the primrary link-map may change this locale.
244 			 */
245 			if ((lml->lm_flags & LML_FLG_BASELM) &&
246 			    ((gptr == 0) || (strcmp(gptr, lptr) != 0))) {
247 				/*
248 				 * If we've obtained a message locale (typically
249 				 * supplied via libc's setlocale()), then
250 				 * register the locale for use in dgettext() so
251 				 * as to reestablish the locale for ld.so.1's
252 				 * messages.
253 				 */
254 				if (gptr) {
255 					free((void *)gptr);
256 					rtld_flags |= RT_FL_NEWLOCALE;
257 				}
258 				glcs[tag].lc_un.lc_ptr = strdup(lptr);
259 
260 				/*
261 				 * Clear any cached messages.
262 				 */
263 				err_strs[ERR_NONE] = 0;
264 				err_strs[ERR_WARNING] = 0;
265 				err_strs[ERR_FATAL] = 0;
266 				err_strs[ERR_ELF] = 0;
267 
268 				nosym_str = 0;
269 			}
270 			break;
271 
272 		case CI_BIND_GUARD:
273 		case CI_BIND_CLEAR:
274 		case CI_THR_SELF:
275 			/*
276 			 * If the global vector is unset, or this is the primary
277 			 * link-map, set the global vector.
278 			 */
279 			if ((gptr == 0) || (lml->lm_flags & LML_FLG_BASELM))
280 				glcs[tag].lc_un.lc_ptr = lptr;
281 
282 			/* FALLTHROUGH */
283 
284 		case CI_TLS_MODADD:
285 		case CI_TLS_MODREM:
286 		case CI_TLS_STATMOD:
287 		case CI_THRINIT:
288 			threaded++;
289 			break;
290 
291 		case CI_VERSION:
292 			if ((rtld_flags2 & RT_FL2_RTLDSEEN) == 0) {
293 				rtld_flags2 |= RT_FL2_RTLDSEEN;
294 
295 				if (funcs->ci_un.ci_val >= CI_V_FOUR) {
296 					Listnode	*lnp;
297 					Lm_list		*lml2;
298 
299 					rtld_flags2 |= RT_FL2_UNIFPROC;
300 
301 					/*
302 					 * We might have seen auditor which is
303 					 * not dependent on libc.  Such an
304 					 * auditor's link map list has
305 					 * LML_FLG_HOLDLOCK set.  This lock
306 					 * needs to be dropped.  Refer to
307 					 * audit_setup() in audit.c.
308 					 */
309 					if ((rtld_flags2 & RT_FL2_HASAUDIT) ==
310 					    0)
311 					break;
312 
313 					/*
314 					 * Yes, we did. Take care of them.
315 					 */
316 					for (LIST_TRAVERSE(&dynlm_list, lnp,
317 					    lml2)) {
318 						Rt_map *map =
319 						    (Rt_map *)lml2->lm_head;
320 
321 						if (FLAGS(map) & FLG_RT_AUDIT) {
322 							lml2->lm_flags &=
323 							    ~LML_FLG_HOLDLOCK;
324 						}
325 					}
326 				}
327 			}
328 			break;
329 
330 		default:
331 			break;
332 		}
333 	}
334 
335 	if (threaded == 0)
336 		return;
337 
338 	/*
339 	 * If a version of libc gives us only a subset of the TLS interfaces -
340 	 * it's confused and we discard the whole lot.
341 	 */
342 	if ((lcp[CI_TLS_MODADD].lc_un.lc_func &&
343 	    lcp[CI_TLS_MODREM].lc_un.lc_func &&
344 	    lcp[CI_TLS_STATMOD].lc_un.lc_func) == 0) {
345 		lcp[CI_TLS_MODADD].lc_un.lc_func = 0;
346 		lcp[CI_TLS_MODREM].lc_un.lc_func = 0;
347 		lcp[CI_TLS_STATMOD].lc_un.lc_func = 0;
348 	}
349 
350 	/*
351 	 * Indicate that we're now thread capable, and enable concurrency if
352 	 * requested.
353 	 */
354 	if ((rtld_flags & RT_FL_NOCONCUR) == 0)
355 		rtld_flags |= RT_FL_CONCUR;
356 	if ((lml->lm_flags & LML_FLG_RTLDLM) == 0)
357 		rtld_flags |= RT_FL_THREADS;
358 }
359 
360 /*
361  * At this point we know we have a set of objects that have been fully analyzed
362  * and relocated.  Prior to the next major step of running .init sections (ie.
363  * running user code), retrieve any RTLDINFO interfaces.
364  */
365 int
366 rt_get_extern(Lm_list *lml, Rt_map *lmp)
367 {
368 	if (lml->lm_rti) {
369 		Aliste		off;
370 		Rti_desc	*rti;
371 
372 		for (ALIST_TRAVERSE(lml->lm_rti, off, rti))
373 			get_lcinterface(rti->rti_lmp, rti->rti_info);
374 
375 		free(lml->lm_rti);
376 		lml->lm_rti = 0;
377 	}
378 
379 	/*
380 	 * Perform some sanity checks.  If we have TLS requirements we better
381 	 * have the associated external interfaces.
382 	 */
383 	if (lml->lm_tls && (lml->lm_lcs[CI_TLS_STATMOD].lc_un.lc_func == 0)) {
384 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_TLS_NOSUPPORT),
385 		    NAME(lmp));
386 		return (0);
387 	}
388 	return (1);
389 }
390 
391 static int	bindmask = 0;
392 
393 int
394 rt_bind_guard(int bindflag)
395 {
396 	int	(*fptr)(int);
397 
398 	if ((fptr = glcs[CI_BIND_GUARD].lc_un.lc_func) != NULL) {
399 		return ((*fptr)(bindflag));
400 	} else {
401 		if ((bindflag & bindmask) == 0) {
402 			bindmask |= bindflag;
403 			return (1);
404 		}
405 		return (0);
406 	}
407 }
408 
409 int
410 rt_bind_clear(int bindflag)
411 {
412 	int	(*fptr)(int);
413 
414 	if ((fptr = glcs[CI_BIND_CLEAR].lc_un.lc_func) != NULL) {
415 		return ((*fptr)(bindflag));
416 	} else {
417 		if (bindflag == 0)
418 			return (bindmask);
419 		else {
420 			bindmask &= ~bindflag;
421 			return (0);
422 		}
423 	}
424 }
425 
426 /*
427  * Make sure threads have been initialized.  This interface is called once for
428  * each link-map list.
429  */
430 void
431 rt_thr_init(Lm_list *lml)
432 {
433 	void	(*fptr)(void);
434 
435 	if ((fptr = (void (*)())lml->lm_lcs[CI_THRINIT].lc_un.lc_func) != 0) {
436 		lml->lm_lcs[CI_THRINIT].lc_un.lc_func = 0;
437 		leave((Lm_list *)0);
438 		(*fptr)();
439 		(void) enter();
440 	}
441 }
442 
443 thread_t
444 rt_thr_self()
445 {
446 	thread_t	(*fptr)(void);
447 
448 	if ((fptr = (thread_t (*)())glcs[CI_THR_SELF].lc_un.lc_func) != NULL)
449 		return ((*fptr)());
450 
451 	return (1);
452 }
453 
454 int
455 rt_mutex_lock(Rt_lock * mp)
456 {
457 	return (_lwp_mutex_lock((lwp_mutex_t *)mp));
458 }
459 
460 int
461 rt_mutex_unlock(Rt_lock * mp)
462 {
463 	return (_lwp_mutex_unlock((lwp_mutex_t *)mp));
464 }
465 
466 Rt_cond *
467 rt_cond_create()
468 {
469 	return (calloc(1, sizeof (Rt_cond)));
470 }
471 
472 int
473 rt_cond_wait(Rt_cond * cvp, Rt_lock * mp)
474 {
475 	return (_lwp_cond_wait(cvp, (lwp_mutex_t *)mp));
476 }
477 
478 int
479 rt_cond_broadcast(Rt_cond * cvp)
480 {
481 	return (_lwp_cond_broadcast(cvp));
482 }
483 
484 #ifdef	EXPAND_RELATIVE
485 
486 /*
487  * Mutex interfaces to resolve references from any objects extracted from
488  * libc_pic.a.  Note, as ld.so.1 is essentially single threaded these can be
489  * noops.
490  */
491 #pragma weak lmutex_lock = __mutex_lock
492 #pragma weak _private_mutex_lock = __mutex_lock
493 #pragma weak mutex_lock = __mutex_lock
494 #pragma weak _mutex_lock = __mutex_lock
495 /* ARGSUSED */
496 int
497 __mutex_lock(mutex_t *mp)
498 {
499 	return (0);
500 }
501 
502 #pragma weak lmutex_unlock = __mutex_unlock
503 #pragma weak _private_mutex_unlock = __mutex_unlock
504 #pragma weak mutex_unlock = __mutex_unlock
505 #pragma weak _mutex_unlock = __mutex_unlock
506 /* ARGSUSED */
507 int
508 __mutex_unlock(mutex_t *mp)
509 {
510 	return (0);
511 }
512 
513 #pragma weak _private_mutex_init = __mutex_init
514 #pragma weak mutex_init = __mutex_init
515 #pragma weak _mutex_init = __mutex_init
516 /* ARGSUSED */
517 int
518 __mutex_init(mutex_t *mp, int type, void *arg)
519 {
520 	return (0);
521 }
522 
523 #pragma weak _private_mutex_destroy = __mutex_destroy
524 #pragma weak mutex_destroy = __mutex_destroy
525 #pragma weak _mutex_destroy = __mutex_destroy
526 /* ARGSUSED */
527 int
528 __mutex_destroy(mutex_t *mp)
529 {
530 	return (0);
531 }
532 
533 /*
534  * This is needed to satisfy sysconf() (case _SC_THREAD_STACK_MIN)
535  */
536 #pragma weak thr_min_stack = _thr_min_stack
537 size_t
538 _thr_min_stack()
539 {
540 #ifdef _LP64
541 	return (8 * 1024);
542 #else
543 	return (4 * 1024);
544 #endif
545 }
546 
547 #endif	/* EXPAND_RELATIVE */
548