xref: /titanic_44/usr/src/cmd/sgs/rtld/common/external.c (revision 98157a7002f4f2cf7978f3084ca5577f0a1d72b2)
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		tag, threaded = 0;
189 	Lm_list		*lml;
190 	Lc_desc		*lcp;
191 
192 	if ((lmp == 0) || (funcs == 0))
193 		return;
194 
195 	lml = LIST(lmp);
196 	lcp = &lml->lm_lcs[0];
197 
198 	DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
199 
200 	for (tag = funcs->ci_tag; tag; tag = (++funcs)->ci_tag) {
201 		char	*gptr;
202 		char	*lptr = funcs->ci_un.ci_ptr;
203 
204 		DBG_CALL(Dbg_util_lcinterface(lmp, tag, lptr));
205 
206 		if (tag >= CI_MAX)
207 			continue;
208 
209 		/*
210 		 * Maintain all interfaces on a per-link-map basis.  Note, for
211 		 * most interfaces, only the first interface is used for any
212 		 * link-map list.  This prevents accidents with developers who
213 		 * manage to load two different versions of libc.
214 		 */
215 		if ((lcp[tag].lc_lmp) &&
216 		    (tag != CI_LCMESSAGES) && (tag != CI_VERSION)) {
217 			DBG_CALL(Dbg_unused_lcinterface(lmp,
218 			    lcp[tag].lc_lmp, tag));
219 			continue;
220 		}
221 
222 		lcp[tag].lc_un.lc_ptr = lptr;
223 		lcp[tag].lc_lmp = lmp;
224 
225 		gptr = glcs[tag].lc_un.lc_ptr;
226 
227 		/*
228 		 * Process any interfaces that must be maintained on a global
229 		 * basis.
230 		 */
231 		switch (tag) {
232 		case CI_ATEXIT:
233 			break;
234 
235 		case CI_LCMESSAGES:
236 			/*
237 			 * At startup, ld.so.1 can establish a locale from one
238 			 * of the locale family of environment variables (see
239 			 * ld_str_env() and readenv_user()).  During process
240 			 * execution the locale can also be changed by the user.
241 			 * This interface is called from libc should the locale
242 			 * be modified.  Presently, only one global locale is
243 			 * maintained for all link-map lists, and only objects
244 			 * on the primrary link-map may change this locale.
245 			 */
246 			if ((lml->lm_flags & LML_FLG_BASELM) &&
247 			    ((gptr == 0) || (strcmp(gptr, lptr) != 0))) {
248 				/*
249 				 * If we've obtained a message locale (typically
250 				 * supplied via libc's setlocale()), then
251 				 * register the locale for use in dgettext() so
252 				 * as to reestablish the locale for ld.so.1's
253 				 * messages.
254 				 */
255 				if (gptr) {
256 					free((void *)gptr);
257 					rtld_flags |= RT_FL_NEWLOCALE;
258 				}
259 				glcs[tag].lc_un.lc_ptr = strdup(lptr);
260 
261 				/*
262 				 * Clear any cached messages.
263 				 */
264 				err_strs[ERR_NONE] = 0;
265 				err_strs[ERR_WARNING] = 0;
266 				err_strs[ERR_FATAL] = 0;
267 				err_strs[ERR_ELF] = 0;
268 
269 				nosym_str = 0;
270 			}
271 			break;
272 
273 		case CI_BIND_GUARD:
274 		case CI_BIND_CLEAR:
275 		case CI_THR_SELF:
276 			/*
277 			 * If the global vector is unset, or this is the primary
278 			 * link-map, set the global vector.
279 			 */
280 			if ((gptr == 0) || (lml->lm_flags & LML_FLG_BASELM))
281 				glcs[tag].lc_un.lc_ptr = lptr;
282 
283 			/* FALLTHROUGH */
284 
285 		case CI_TLS_MODADD:
286 		case CI_TLS_MODREM:
287 		case CI_TLS_STATMOD:
288 		case CI_THRINIT:
289 			threaded++;
290 			break;
291 
292 		case CI_VERSION:
293 			if ((rtld_flags2 & RT_FL2_RTLDSEEN) == 0) {
294 				rtld_flags2 |= RT_FL2_RTLDSEEN;
295 
296 				if (funcs->ci_un.ci_val >= CI_V_FOUR) {
297 					Listnode	*lnp;
298 					Lm_list		*lml2;
299 
300 					rtld_flags2 |= RT_FL2_UNIFPROC;
301 
302 					/*
303 					 * We might have seen auditor which is
304 					 * not dependent on libc.  Such an
305 					 * auditor's link map list has
306 					 * LML_FLG_HOLDLOCK set.  This lock
307 					 * needs to be dropped.  Refer to
308 					 * audit_setup() in audit.c.
309 					 */
310 					if ((rtld_flags2 & RT_FL2_HASAUDIT) ==
311 					    0)
312 					break;
313 
314 					/*
315 					 * Yes, we did. Take care of them.
316 					 */
317 					for (LIST_TRAVERSE(&dynlm_list, lnp,
318 					    lml2)) {
319 						Rt_map *map =
320 						    (Rt_map *)lml2->lm_head;
321 
322 						if (FLAGS(map) & FLG_RT_AUDIT) {
323 							lml2->lm_flags &=
324 							    ~LML_FLG_HOLDLOCK;
325 						}
326 					}
327 				}
328 			}
329 			break;
330 
331 		default:
332 			break;
333 		}
334 	}
335 
336 	if (threaded == 0)
337 		return;
338 
339 	/*
340 	 * If a version of libc gives us only a subset of the TLS interfaces -
341 	 * it's confused and we discard the whole lot.
342 	 */
343 	if ((lcp[CI_TLS_MODADD].lc_un.lc_func &&
344 	    lcp[CI_TLS_MODREM].lc_un.lc_func &&
345 	    lcp[CI_TLS_STATMOD].lc_un.lc_func) == 0) {
346 		lcp[CI_TLS_MODADD].lc_un.lc_func = 0;
347 		lcp[CI_TLS_MODREM].lc_un.lc_func = 0;
348 		lcp[CI_TLS_STATMOD].lc_un.lc_func = 0;
349 	}
350 
351 	/*
352 	 * Indicate that we're now thread capable, and enable concurrency if
353 	 * requested.
354 	 */
355 	if ((rtld_flags & RT_FL_NOCONCUR) == 0)
356 		rtld_flags |= RT_FL_CONCUR;
357 	if ((lml->lm_flags & LML_FLG_RTLDLM) == 0)
358 		rtld_flags |= RT_FL_THREADS;
359 }
360 
361 /*
362  * At this point we know we have a set of objects that have been fully analyzed
363  * and relocated.  Prior to the next major step of running .init sections (ie.
364  * running user code), retrieve any RTLDINFO interfaces.
365  */
366 int
367 rt_get_extern(Lm_list *lml, Rt_map *lmp)
368 {
369 	if (lml->lm_rti) {
370 		Aliste		idx;
371 		Rti_desc	*rti;
372 
373 		for (ALIST_TRAVERSE(lml->lm_rti, idx, rti))
374 			get_lcinterface(rti->rti_lmp, rti->rti_info);
375 
376 		free(lml->lm_rti);
377 		lml->lm_rti = 0;
378 	}
379 
380 	/*
381 	 * Perform some sanity checks.  If we have TLS requirements we better
382 	 * have the associated external interfaces.
383 	 */
384 	if (lml->lm_tls && (lml->lm_lcs[CI_TLS_STATMOD].lc_un.lc_func == 0)) {
385 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_TLS_NOSUPPORT),
386 		    NAME(lmp));
387 		return (0);
388 	}
389 	return (1);
390 }
391 
392 static int	bindmask = 0;
393 
394 int
395 rt_bind_guard(int bindflag)
396 {
397 	int	(*fptr)(int);
398 
399 	if ((fptr = glcs[CI_BIND_GUARD].lc_un.lc_func) != NULL) {
400 		return ((*fptr)(bindflag));
401 	} else {
402 		if ((bindflag & bindmask) == 0) {
403 			bindmask |= bindflag;
404 			return (1);
405 		}
406 		return (0);
407 	}
408 }
409 
410 int
411 rt_bind_clear(int bindflag)
412 {
413 	int	(*fptr)(int);
414 
415 	if ((fptr = glcs[CI_BIND_CLEAR].lc_un.lc_func) != NULL) {
416 		return ((*fptr)(bindflag));
417 	} else {
418 		if (bindflag == 0)
419 			return (bindmask);
420 		else {
421 			bindmask &= ~bindflag;
422 			return (0);
423 		}
424 	}
425 }
426 
427 /*
428  * Make sure threads have been initialized.  This interface is called once for
429  * each link-map list.
430  */
431 void
432 rt_thr_init(Lm_list *lml)
433 {
434 	void	(*fptr)(void);
435 
436 	if ((fptr = (void (*)())lml->lm_lcs[CI_THRINIT].lc_un.lc_func) != 0) {
437 		lml->lm_lcs[CI_THRINIT].lc_un.lc_func = 0;
438 		leave((Lm_list *)0);
439 		(*fptr)();
440 		(void) enter();
441 	}
442 }
443 
444 thread_t
445 rt_thr_self()
446 {
447 	thread_t	(*fptr)(void);
448 
449 	if ((fptr = (thread_t (*)())glcs[CI_THR_SELF].lc_un.lc_func) != NULL)
450 		return ((*fptr)());
451 
452 	return (1);
453 }
454 
455 int
456 rt_mutex_lock(Rt_lock * mp)
457 {
458 	return (_lwp_mutex_lock((lwp_mutex_t *)mp));
459 }
460 
461 int
462 rt_mutex_unlock(Rt_lock * mp)
463 {
464 	return (_lwp_mutex_unlock((lwp_mutex_t *)mp));
465 }
466 
467 Rt_cond *
468 rt_cond_create()
469 {
470 	return (calloc(1, sizeof (Rt_cond)));
471 }
472 
473 int
474 rt_cond_wait(Rt_cond * cvp, Rt_lock * mp)
475 {
476 	return (_lwp_cond_wait(cvp, (lwp_mutex_t *)mp));
477 }
478 
479 int
480 rt_cond_broadcast(Rt_cond * cvp)
481 {
482 	return (_lwp_cond_broadcast(cvp));
483 }
484 
485 #ifdef	EXPAND_RELATIVE
486 
487 /*
488  * Mutex interfaces to resolve references from any objects extracted from
489  * libc_pic.a.  Note, as ld.so.1 is essentially single threaded these can be
490  * noops.
491  */
492 #pragma weak lmutex_lock = __mutex_lock
493 #pragma weak _private_mutex_lock = __mutex_lock
494 #pragma weak mutex_lock = __mutex_lock
495 #pragma weak _mutex_lock = __mutex_lock
496 /* ARGSUSED */
497 int
498 __mutex_lock(mutex_t *mp)
499 {
500 	return (0);
501 }
502 
503 #pragma weak lmutex_unlock = __mutex_unlock
504 #pragma weak _private_mutex_unlock = __mutex_unlock
505 #pragma weak mutex_unlock = __mutex_unlock
506 #pragma weak _mutex_unlock = __mutex_unlock
507 /* ARGSUSED */
508 int
509 __mutex_unlock(mutex_t *mp)
510 {
511 	return (0);
512 }
513 
514 #pragma weak _private_mutex_init = __mutex_init
515 #pragma weak mutex_init = __mutex_init
516 #pragma weak _mutex_init = __mutex_init
517 /* ARGSUSED */
518 int
519 __mutex_init(mutex_t *mp, int type, void *arg)
520 {
521 	return (0);
522 }
523 
524 #pragma weak _private_mutex_destroy = __mutex_destroy
525 #pragma weak mutex_destroy = __mutex_destroy
526 #pragma weak _mutex_destroy = __mutex_destroy
527 /* ARGSUSED */
528 int
529 __mutex_destroy(mutex_t *mp)
530 {
531 	return (0);
532 }
533 
534 /*
535  * This is needed to satisfy sysconf() (case _SC_THREAD_STACK_MIN)
536  */
537 #pragma weak thr_min_stack = _thr_min_stack
538 size_t
539 _thr_min_stack()
540 {
541 #ifdef _LP64
542 	return (8 * 1024);
543 #else
544 	return (4 * 1024);
545 #endif
546 }
547 
548 /*
549  * The following functions are cancellation points in libc.
550  * They are called from other functions in libc that we extract
551  * and use directly.  We don't do cancellation while we are in
552  * the dynamic linker, so we redefine these to call the primitive,
553  * non-cancellation interfaces.
554  */
555 
556 #pragma weak close = _close
557 int
558 _close(int fildes)
559 {
560 	extern int __close(int);
561 
562 	return (__close(fildes));
563 }
564 
565 #pragma weak fcntl = _fcntl
566 int
567 _fcntl(int fildes, int cmd, ...)
568 {
569 	extern int __fcntl(int, int, ...);
570 	intptr_t arg;
571 	va_list ap;
572 
573 	va_start(ap, cmd);
574 	arg = va_arg(ap, intptr_t);
575 	va_end(ap);
576 	return (__fcntl(fildes, cmd, arg));
577 }
578 
579 #pragma weak open = _open
580 int
581 _open(const char *path, int oflag, ...)
582 {
583 	extern int __open(const char *, int, ...);
584 	mode_t mode;
585 	va_list ap;
586 
587 	va_start(ap, oflag);
588 	mode = va_arg(ap, mode_t);
589 	va_end(ap);
590 	return (__open(path, oflag, mode));
591 }
592 
593 #pragma weak openat = _openat
594 int
595 _openat(int fd, const char *path, int oflag, ...)
596 {
597 	extern int __openat(int, const char *, int, ...);
598 	mode_t mode;
599 	va_list ap;
600 
601 	va_start(ap, oflag);
602 	mode = va_arg(ap, mode_t);
603 	va_end(ap);
604 	return (__openat(fd, path, oflag, mode));
605 }
606 
607 #pragma weak read = _read
608 ssize_t
609 _read(int fd, void *buf, size_t size)
610 {
611 	extern ssize_t __read(int, void *, size_t);
612 	return (__read(fd, buf, size));
613 }
614 
615 #pragma weak write = _write
616 ssize_t
617 _write(int fd, const void *buf, size_t size)
618 {
619 	extern ssize_t __write(int, const void *, size_t);
620 	return (__write(fd, buf, size));
621 }
622 
623 #endif	/* EXPAND_RELATIVE */
624