feature_tests.h: always use largefile interfaces
[unleashed/lotheac.git] / usr / src / cmd / sgs / rtld / common / external.c
blob3a2104ebc1cd9901fbc029c51b6b3d75efb2847e
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
25 * Copyright 2016 Joyent, Inc.
29 * Implementation of all external interfaces between ld.so.1 and libc.
31 * This file started as a set of routines that provided synchronization and
32 * locking operations using calls to libthread. libthread has merged with libc
33 * under the Unified Process Model (UPM), and things have gotten a lot simpler.
34 * This file continues to establish and redirect various events within ld.so.1
35 * to interfaces within libc.
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.
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).
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.
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.
57 * These events are established by:
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.
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.
73 * iii. Calls to CI_TLS_STATMOD, and CI_THRINIT occur for the primary link-map
74 * list in the final stages of setup().
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:
81 * CI_ATEXIT
82 * CI TLS_MODADD
83 * CI_TLS_MODREM
84 * CI_TLS_STATMOD
85 * CI_THRINIT
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:
93 * CI_LCMESSAGES
94 * CI_BIND_GUARD
95 * CI_BIND_CLEAR
96 * CI_THR_SELF
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.
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.
111 * The history of these external interfaces is defined by their version:
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).
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).
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.
134 * CI_VERSION == 1
135 * Introduced with CI_VERSION & CI_ATEXIT
137 * CI_VERSION == 2 (Solaris 8 update 2).
138 * Added support for CI_LCMESSAGES
140 * CI_VERSION == 3 (Solaris 9).
141 * Added the following versions to the CI table:
143 * CI_BIND_GUARD, CI_BIND_CLEAR, CI_THR_SELF
144 * CI_TLS_MODADD, CI_TLS_MOD_REMOVE, CI_TLS_STATMOD
146 * This version introduced the DT_SUNW_RTLDINFO structure as a mechanism
147 * to handshake with ld.so.1.
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).
155 * CI_VERSION == 5 (Solaris 11).
156 * Use of "protected" references within libc, so that symbols are
157 * pre-bound, and don't require ld.so.1 binding. This implementation
158 * protects libc's critical regions from being vectored to auditors.
160 * CI_VERSION == 6 (Solaris 11).
161 * Added the CI_CRITICAL handshake, to allow "mem*" family to be reexposed
162 * as "global", and thus be redirected to auxiliary filters.
164 * Release summary:
166 * Solaris 8 CI_ATEXIT via _ld_libc()
167 * TI_* via _ld_concurrency()
169 * Solaris 9 CI_ATEXIT and CI_LCMESSAGES via _ld_libc()
170 * CI_* via RTLDINFO and _ld_libc() - new libthread
171 * TI_* via _ld_concurrency() - old libthread
173 * Solaris 10 CI_ATEXIT and CI_LCMESSAGES via _ld_libc()
174 * CI_* via RTLDINFO and _ld_libc() - new libthread
177 #include <sys/syscall.h>
178 #include <sys/debug.h>
179 #include <synch.h>
180 #include <signal.h>
181 #include <thread.h>
182 #include <synch.h>
183 #include <strings.h>
184 #include <stdio.h>
185 #include <libintl.h>
186 #include <debug.h>
187 #include <libc_int.h>
188 #include "_elf.h"
189 #include "_rtld.h"
192 * This interface provides the unified process model communication between
193 * ld.so.1 and libc. This interface can be called a number of times:
195 * - Initially, this interface is called to process RTLDINFO. This data
196 * structure is typically provided by libc, and contains the address of
197 * libc interfaces that must be called to initialize threads information.
199 * - _ld_libc(), this interface can also be called by libc at process
200 * initialization, after libc has been loaded and relocated, but before
201 * control has been passed to any user code (.init's or main()). This
202 * call provides additional libc interface information that ld.so.1 must
203 * call during process execution.
205 * - _ld_libc() can also be called by libc during process execution to
206 * re-establish interfaces such as the locale.
208 static void
209 get_lcinterface(Rt_map *lmp, Lc_interface *funcs)
211 int threaded = 0, entry = 0, tag;
212 Lm_list *lml;
213 Lc_desc *lcp;
215 if ((lmp == NULL) || (funcs == NULL))
216 return;
219 * Once the process is active, ensure we grab a lock.
221 if (rtld_flags & RT_FL_APPLIC)
222 entry = enter(0);
224 lml = LIST(lmp);
225 lcp = &lml->lm_lcs[0];
227 DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
229 for (tag = funcs->ci_tag; tag; tag = (++funcs)->ci_tag) {
230 char *gptr;
231 char *lptr = funcs->ci_un.ci_ptr;
233 DBG_CALL(Dbg_util_lcinterface(lmp, tag, lptr));
235 if (tag >= CI_MAX)
236 continue;
239 * Maintain all interfaces on a per-link-map basis. Note, for
240 * most interfaces, only the first interface is used for any
241 * link-map list. This prevents accidents with developers who
242 * manage to load two different versions of libc.
244 if ((lcp[tag].lc_lmp) &&
245 (tag != CI_LCMESSAGES) && (tag != CI_VERSION)) {
246 DBG_CALL(Dbg_unused_lcinterface(lmp,
247 lcp[tag].lc_lmp, tag));
248 continue;
251 lcp[tag].lc_un.lc_ptr = lptr;
252 lcp[tag].lc_lmp = lmp;
254 gptr = glcs[tag].lc_un.lc_ptr;
257 * Process any interfaces that must be maintained on a global
258 * basis.
260 switch (tag) {
261 case CI_ATEXIT:
262 break;
264 case CI_LCMESSAGES:
266 * At startup, ld.so.1 can establish a locale from one
267 * of the locale family of environment variables (see
268 * ld_str_env() and readenv_user()). During process
269 * execution the locale can also be changed by the user.
270 * This interface is called from libc should the locale
271 * be modified. Presently, only one global locale is
272 * maintained for all link-map lists, and only objects
273 * on the primrary link-map may change this locale.
275 if ((lml->lm_flags & LML_FLG_BASELM) &&
276 ((gptr == NULL) || (strcmp(gptr, lptr) != 0))) {
278 * If we've obtained a message locale (typically
279 * supplied via libc's setlocale()), then
280 * register the locale for use in dgettext() so
281 * as to reestablish the locale for ld.so.1's
282 * messages.
284 if (gptr) {
285 free((void *)gptr);
286 rtld_flags |= RT_FL_NEWLOCALE;
288 glcs[tag].lc_un.lc_ptr = strdup(lptr);
291 * Clear any cached messages.
293 bzero(err_strs, sizeof (err_strs));
294 nosym_str = NULL;
296 break;
298 case CI_BIND_GUARD:
299 case CI_BIND_CLEAR:
300 case CI_THR_SELF:
301 case CI_CRITICAL:
303 * If the global vector is unset, or this is the primary
304 * link-map, set the global vector.
306 if ((gptr == NULL) || (lml->lm_flags & LML_FLG_BASELM))
307 glcs[tag].lc_un.lc_ptr = lptr;
309 /* FALLTHROUGH */
311 case CI_TLS_MODADD:
312 case CI_TLS_MODREM:
313 case CI_TLS_STATMOD:
314 case CI_THRINIT:
315 threaded++;
316 break;
318 case CI_VERSION:
319 if ((rtld_flags2 & RT_FL2_RTLDSEEN) == 0) {
320 Aliste idx;
321 Lm_list *lml2;
322 int version;
324 rtld_flags2 |= RT_FL2_RTLDSEEN;
326 version = funcs->ci_un.ci_val;
327 #if defined(CI_V_FIVE)
328 if (version >= CI_V_FIVE) {
329 thr_flg_nolock = THR_FLG_NOLOCK;
330 thr_flg_reenter = THR_FLG_REENTER;
332 #endif
333 if (version < CI_V_FOUR)
334 break;
336 rtld_flags2 |= RT_FL2_UNIFPROC;
339 * We might have seen an auditor which is not
340 * dependent on libc. Such an auditor's link
341 * map list has LML_FLG_HOLDLOCK set. This
342 * lock needs to be dropped. Refer to
343 * audit_setup() in audit.c.
345 if ((rtld_flags2 & RT_FL2_HASAUDIT) == 0)
346 break;
349 * Yes, we did. Take care of them.
351 for (APLIST_TRAVERSE(dynlm_list, idx, lml2)) {
352 Rt_map *map = (Rt_map *)lml2->lm_head;
354 if (FLAGS(map) & FLG_RT_AUDIT) {
355 lml2->lm_flags &=
356 ~LML_FLG_HOLDLOCK;
360 break;
362 default:
363 break;
367 if (threaded) {
369 * If a version of libc gives us only a subset of the TLS
370 * interfaces, it's confused and we discard the whole lot.
372 if ((lcp[CI_TLS_MODADD].lc_un.lc_func &&
373 lcp[CI_TLS_MODREM].lc_un.lc_func &&
374 lcp[CI_TLS_STATMOD].lc_un.lc_func) == 0) {
375 lcp[CI_TLS_MODADD].lc_un.lc_func = NULL;
376 lcp[CI_TLS_MODREM].lc_un.lc_func = NULL;
377 lcp[CI_TLS_STATMOD].lc_un.lc_func = NULL;
381 * Indicate that we're now thread capable.
383 if ((lml->lm_flags & LML_FLG_RTLDLM) == 0)
384 rtld_flags |= RT_FL_THREADS;
387 if (entry)
388 leave(lml, 0);
392 * At this point we know we have a set of objects that have been fully analyzed
393 * and relocated. Prior to the next major step of running .init sections (ie.
394 * running user code), retrieve any RTLDINFO interfaces.
397 rt_get_extern(Lm_list *lml, Rt_map *lmp)
399 if (lml->lm_rti) {
400 Aliste idx;
401 Rti_desc *rti;
403 for (ALIST_TRAVERSE(lml->lm_rti, idx, rti))
404 get_lcinterface(rti->rti_lmp, rti->rti_info);
406 free(lml->lm_rti);
407 lml->lm_rti = 0;
411 * Perform some sanity checks. If we have TLS requirements we better
412 * have the associated external interfaces.
414 if (lml->lm_tls &&
415 (lml->lm_lcs[CI_TLS_STATMOD].lc_un.lc_func == NULL)) {
416 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_TLS_NOSUPPORT),
417 NAME(lmp));
418 return (0);
420 return (1);
424 * Provide an interface for libc to communicate additional interface
425 * information.
427 void
428 _ld_libc(void *ptr)
430 get_lcinterface(_caller(caller(), CL_EXECDEF), (Lc_interface *)ptr);
433 static int bindmask = 0;
436 rt_bind_guard(int flags)
438 int (*fptr)(int);
439 int bindflag;
441 if ((fptr = glcs[CI_BIND_GUARD].lc_un.lc_func) != NULL) {
442 return ((*fptr)(flags));
443 } else {
444 bindflag = (flags & THR_FLG_RTLD);
445 if ((bindflag & bindmask) == 0) {
446 bindmask |= bindflag;
447 return (1);
449 return (0);
454 rt_bind_clear(int flags)
456 int (*fptr)(int);
457 int bindflag;
459 if ((fptr = glcs[CI_BIND_CLEAR].lc_un.lc_func) != NULL) {
460 return ((*fptr)(flags));
461 } else {
462 bindflag = (flags & THR_FLG_RTLD);
463 if (bindflag == 0)
464 return (bindmask);
465 else {
466 bindmask &= ~bindflag;
467 return (0);
473 * Make sure threads have been initialized. This interface is called once for
474 * each link-map list.
476 void
477 rt_thr_init(Lm_list *lml)
479 void (*fptr)(void);
481 if ((fptr =
482 (void (*)())lml->lm_lcs[CI_THRINIT].lc_un.lc_func) != NULL) {
483 lml->lm_lcs[CI_THRINIT].lc_un.lc_func = NULL;
485 leave(lml, thr_flg_reenter);
486 (*fptr)();
487 (void) enter(thr_flg_reenter);
490 * If this is an alternative link-map list, and this is the
491 * first call to initialize threads, don't let the destination
492 * libc be deleted. It is possible that an auditors complete
493 * initialization fails, but there is presently no main link-map
494 * list. As this libc has established the thread pointer, don't
495 * delete this libc, otherwise the initialization of libc on the
496 * main link-map can be compromised during its threads
497 * initialization.
499 if (((lml->lm_flags & LML_FLG_BASELM) == 0) &&
500 ((rtld_flags2 & RT_FL2_PLMSETUP) == 0))
501 MODE(lml->lm_lcs[CI_THRINIT].lc_lmp) |= RTLD_NODELETE;
505 thread_t
506 rt_thr_self()
508 thread_t (*fptr)(void);
510 if ((fptr = (thread_t (*)())glcs[CI_THR_SELF].lc_un.lc_func) != NULL)
511 return ((*fptr)());
513 return (1);
517 rt_mutex_lock(Rt_lock *mp)
519 return (_lwp_mutex_lock((lwp_mutex_t *)mp));
523 rt_mutex_unlock(Rt_lock *mp)
525 return (_lwp_mutex_unlock((lwp_mutex_t *)mp));
529 * Test whether we're in a libc critical region. Certain function references,
530 * like the "mem*" family, might require binding. Although these functions can
531 * safely bind to auxiliary filtees, they should not be captured by auditors.
534 rt_critical()
536 int (*fptr)(void);
538 if ((fptr = glcs[CI_CRITICAL].lc_un.lc_func) != NULL)
539 return ((*fptr)());
541 return (0);
545 * Mutex interfaces to resolve references from any objects extracted from
546 * libc_pic.a. Note, as ld.so.1 is essentially single threaded these can be
547 * noops.
549 #pragma weak lmutex_lock = mutex_lock
550 /* ARGSUSED */
552 mutex_lock(mutex_t *mp)
554 return (0);
557 #pragma weak lmutex_unlock = mutex_unlock
558 /* ARGSUSED */
560 mutex_unlock(mutex_t *mp)
562 return (0);
565 /* ARGSUSED */
567 mutex_init(mutex_t *mp, int type, void *arg)
569 return (0);
572 /* ARGSUSED */
574 mutex_destroy(mutex_t *mp)
576 return (0);
580 * This is needed to satisfy sysconf() (case _SC_THREAD_STACK_MIN)
582 size_t
583 thr_min_stack()
585 return (sizeof (uintptr_t) * 1024);
589 * Local str[n]casecmp() interfaces for the dynamic linker,
590 * to avoid problems when linking with libc_pic.a
593 strcasecmp(const char *s1, const char *s2)
595 extern int ascii_strcasecmp(const char *, const char *);
597 return (ascii_strcasecmp(s1, s2));
601 strncasecmp(const char *s1, const char *s2, size_t n)
603 extern int ascii_strncasecmp(const char *, const char *, size_t);
605 return (ascii_strncasecmp(s1, s2, n));
609 * The following functions are cancellation points in libc.
610 * They are called from other functions in libc that we extract
611 * and use directly. We don't do cancellation while we are in
612 * the dynamic linker, so we redefine these to call the primitive,
613 * non-cancellation interfaces.
616 close(int fildes)
618 extern int __close(int);
620 return (__close(fildes));
624 fcntl(int fildes, int cmd, ...)
626 extern int __fcntl(int, int, ...);
627 intptr_t arg;
628 va_list ap;
630 va_start(ap, cmd);
631 arg = va_arg(ap, intptr_t);
632 va_end(ap);
633 return (__fcntl(fildes, cmd, arg));
636 #ifndef _LP64
638 * XXX always-largefile hack: open/openat are special: they are defined in
639 * external.c, and we want to use those. but the system headers have
640 * #redefine_extname'd them to the *64 versions. so for 32-bit, define the *64
641 * funcs here.
643 #define open open64
644 #define openat openat64
645 #endif
648 open(const char *path, int oflag, ...)
650 extern int __open(const char *, int, mode_t);
651 mode_t mode;
652 va_list ap;
654 va_start(ap, oflag);
655 mode = va_arg(ap, mode_t);
656 va_end(ap);
657 return (__open(path, oflag, mode));
661 openat(int fd, const char *path, int oflag, ...)
663 extern int __openat(int, const char *, int, mode_t);
664 mode_t mode;
665 va_list ap;
667 va_start(ap, oflag);
668 mode = va_arg(ap, mode_t);
669 va_end(ap);
670 return (__openat(fd, path, oflag, mode));
673 ssize_t
674 read(int fd, void *buf, size_t size)
676 extern ssize_t __read(int, void *, size_t);
677 return (__read(fd, buf, size));
680 ssize_t
681 write(int fd, const void *buf, size_t size)
683 extern ssize_t __write(int, const void *, size_t);
684 return (__write(fd, buf, size));
688 * ASCII versions of ctype character classification functions. This avoids
689 * pulling in the entire locale framework that is in libc.
693 isdigit(int c)
695 return ((c >= '0' && c <= '9') ? 1 : 0);
699 isupper(int c)
701 return ((c >= 'A' && c <= 'Z') ? 1 : 0);
705 islower(int c)
707 return ((c >= 'a' && c <= 'z') ? 1 : 0);
711 isspace(int c)
713 return (((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n') ||
714 (c == '\v') || (c == '\f')) ? 1 : 0);
718 isxdigit(int c)
720 return ((isdigit(c) || (c >= 'A' && c <= 'F') ||
721 (c >= 'a' && c <= 'f')) ? 1 : 0);
725 isalpha(int c)
727 return ((isupper(c) || islower(c)) ? 1 : 0);
731 isalnum(int c)
733 return ((isalpha(c) || isdigit(c)) ? 1 : 0);
736 #if defined(__i386) || defined(__amd64)
738 * Instead of utilizing the comm page for clock_gettime, rtld uses the raw
739 * syscall instead. Doing so decreases the surface of symbols needed from libc
740 * for a modest performance cost.
742 extern int __clock_gettime_sys(clockid_t, struct timespec *);
745 __clock_gettime(clockid_t clock_id, struct timespec *tp)
747 return (__clock_gettime_sys(clock_id, tp));
749 #endif /* defined(__i386) || defined(__amd64) */
752 * The libc wrapper for sysinfo can't be used here since it uses
753 * strlcpy/getenv, so shim it with syscall() here.
756 sysinfo(int command, char *buf, long count)
758 return syscall(SYS_systeminfo, command, buf, count);
762 * In a similar vein to the is* functions above, we also have to define our own
763 * version of strerror, as it is implemented in terms of the locale aware
764 * strerror_l, and we'd rather not have the full set of libc symbols used here.
766 extern const char _sys_errs[];
767 extern const int _sys_index[];
768 extern int _sys_num_err;
770 char *
771 strerror(int errnum)
773 if (errnum < _sys_num_err && errnum >= 0) {
774 return (dgettext("SUNW_OST_OSLIB",
775 (char *)&_sys_errs[_sys_index[errnum]]));
778 errno = EINVAL;
779 return (dgettext("SUNW_OST_OSLIB", "Unknown error"));