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