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