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