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