xref: /illumos-gate/usr/src/cmd/sgs/rtld/common/external.c (revision e4e529b2beb8a30e14beb0573c4a114275d157cc)
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 (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
25  * Copyright (c) 2017, Joyent, Inc.
26  * Copyright 2025 Oxide Computer Company
27  */
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  * under the Unified Process Model (UPM), and things have gotten a lot simpler.
35  * This file continues to establish and redirect various events within ld.so.1
36  * to interfaces within libc.
37  *
38  * Until libc is loaded and relocated, any external interfaces are captured
39  * locally.  Each link-map list maintains its own set of external vectors, as
40  * each link-map list typically provides its own libc.  Although this per-link-
41  * map list vectoring provides a degree of flexibility, there is a protocol
42  * expected when calling various libc interfaces.
43  *
44  * i.	Any new alternative link-map list should call CI_THRINIT, and then call
45  *	CI_TLS_MODADD to register any TLS for each object of that link-map list
46  *	(this item is labeled i. as auditors can be the first objects loaded,
47  *	and they exist on their own lik-map list).
48  *
49  * ii.	For the primary link-map list, CI_TLS_STATMOD must be called first to
50  *	register any static TLS.  This routine is called regardless of there
51  *	being any TLS, as this routine also establishes the link-map list as the
52  *	primary list and fixes the association of uberdata).  CI_THRINIT should
53  *	then be called.
54  *
55  * iii.	Any objects added to an existing link-map list (primary or alternative)
56  *	should call CI_TLS_MODADD to register any additional TLS.
57  *
58  * These events are established by:
59  *
60  * i.	Typically, libc is loaded as part of the primary dependencies of any
61  *	link-map list (since the Unified Process Model (UPM), libc can't be
62  *	lazily loaded).  To minimize the possibility of loading and registering
63  *	objects, and then tearing them down (because of a relocation error),
64  *	external vectors are established as part of load_completion().  This
65  *	routine is called on completion of any operation that can cause objects
66  *	to be loaded.  This point of control insures the objects have been fully
67  *	analyzed and relocated, and moved to their controlling link-map list.
68  *	The external vectors are established prior to any .inits being fired.
69  *
70  * ii.	Calls to CI_THRINIT, and CI_TLS_MODADD also occur as part of
71  *	load_completion().  CI_THRINIT is only called once for each link-map
72  *	control list.
73  *
74  * iii.	Calls to CI_TLS_STATMOD, and CI_THRINIT occur for the primary link-map
75  *	list in the final stages of setup().
76  *
77  * The interfaces provide by libc can be divided into two families.  The first
78  * family consists of those interfaces that should be called from the link-map
79  * list.  It's possible that these interfaces convey state concerning the
80  * link-map list they are part of:
81  *
82  *	CI_ATEXIT
83  *	CI TLS_MODADD
84  *	CI_TLS_MODREM
85  *	CI_TLS_STATMOD
86  *	CI_THRINIT
87  *
88  * The second family are global in nature, that is, the link-map list from
89  * which they are called provides no state information.  In fact, for
90  * CI_BIND_GUARD, the calling link-map isn't even known.  The link-map can only
91  * be deduced after ld.so.1's global lock has been obtained.  Therefore, the
92  * following interfaces are also maintained as global:
93  *
94  *	CI_LCMESSAGES
95  *	CI_BIND_GUARD
96  *	CI_BIND_CLEAR
97  *	CI_THR_SELF
98  *
99  * Note, it is possible that these global interfaces are obtained from an
100  * alternative link-map list that gets torn down because of a processing
101  * failure (unlikely, because the link-map list components must be analyzed
102  * and relocated prior to load_completion(), but perhaps the tear down is still
103  * a possibility).  Thus the global interfaces may have to be replaced.  Once
104  * the interfaces have been obtained from the primary link-map, they can
105  * remain fixed, as the primary link-map isn't going to go anywhere.
106  *
107  * The last wrinkle in the puzzle is what happens if an alternative link-map
108  * is loaded with no libc dependency?  In this case, the alternative objects
109  * can not call CI_THRINIT, can not be allowed to use TLS, and will not receive
110  * any atexit processing.
111  *
112  * The history of these external interfaces is defined by their version:
113  *
114  * TI_VERSION == 1
115  *	Under this model libthread provided rw_rwlock/rw_unlock, through which
116  *	all rt_mutex_lock/rt_mutex_unlock calls were vectored.
117  *	Under libc/libthread these interfaces provided _sigon/_sigoff (unlike
118  *	lwp/libthread that provided signal blocking via bind_guard/bind_clear).
119  *
120  * TI_VERSION == 2
121  *	Under this model only libthreads bind_guard/bind_clear and thr_self
122  *	interfaces were used.  Both libthreads blocked signals under the
123  *	bind_guard/bind_clear interfaces.   Lower level locking is derived
124  *	from internally bound _lwp_ interfaces.  This removes recursive
125  *	problems encountered when obtaining locking interfaces from libthread.
126  *	The use of mutexes over reader/writer locks also enables the use of
127  *	condition variables for controlling thread concurrency (allows access
128  *	to objects only after their .init has completed).
129  *
130  * NOTE, the TI_VERSION indicated the ti_interface version number, where the
131  * ti_interface was a large vector of functions passed to both libc (to override
132  * the thread stub interfaces) and ld.so.1.  ld.so.1 used only a small subset of
133  * these interfaces.
134  *
135  * CI_VERSION == 1
136  *	Introduced with CI_VERSION & CI_ATEXIT
137  *
138  * CI_VERSION == 2 (Solaris 8 update 2).
139  *	Added support for CI_LCMESSAGES
140  *
141  * CI_VERSION == 3 (Solaris 9).
142  *	Added the following versions to the CI table:
143  *
144  *		CI_BIND_GUARD, CI_BIND_CLEAR, CI_THR_SELF
145  *		CI_TLS_MODADD, CI_TLS_MOD_REMOVE, CI_TLS_STATMOD
146  *
147  *	This version introduced the DT_SUNW_RTLDINFO structure as a mechanism
148  *	to handshake with ld.so.1.
149  *
150  * CI_VERSION == 4 (Solaris 10).
151  *	Added the CI_THRINIT handshake as part of the libc/libthread unified
152  *	process model.  libc now initializes the current thread pointer from
153  *	this interface (and no longer relies on the INITFIRST flag - which
154  *	others have started to camp out on).
155  *
156  * CI_VERSION == 5 (Solaris 11).
157  *	Use of "protected" references within libc, so that symbols are
158  *	pre-bound, and don't require ld.so.1 binding.  This implementation
159  *	protects libc's critical regions from being vectored to auditors.
160  *
161  * CI_VERSION == 6 (Solaris 11).
162  *	Added the CI_CRITICAL handshake, to allow "mem*" family to be reexposed
163  *	as "global", and thus be redirected to auxiliary filters.
164  *
165  * Release summary:
166  *
167  *	Solaris 8	CI_ATEXIT via _ld_libc()
168  *			TI_* via _ld_concurrency()
169  *
170  *	Solaris 9	CI_ATEXIT and CI_LCMESSAGES via _ld_libc()
171  *			CI_* via RTLDINFO and _ld_libc()  - new libthread
172  *			TI_* via _ld_concurrency()  - old libthread
173  *
174  *	Solaris 10	CI_ATEXIT and CI_LCMESSAGES via _ld_libc()
175  *			CI_* via RTLDINFO and _ld_libc()  - new libthread
176  */
177 
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 <syserr.h>
189 #include <fcntl.h>
190 #include "_elf.h"
191 #include "_rtld.h"
192 
193 /*
194  * This interface provides the unified process model communication between
195  * ld.so.1 and libc.  This interface can be called a number of times:
196  *
197  *   -	Initially, this interface is called to process RTLDINFO.  This data
198  *	structure is typically provided by libc, and contains the address of
199  *	libc interfaces that must be called to initialize threads information.
200  *
201  *   -	_ld_libc(), this interface can also be called by libc at process
202  *	initialization, after libc has been loaded and relocated, but before
203  *	control has been passed to any user code (.init's or main()).  This
204  *	call provides additional libc interface information that ld.so.1 must
205  *	call during process execution.
206  *
207  *   -	_ld_libc() can also be called by libc during process execution to
208  *	re-establish interfaces such as the locale.
209  */
210 static void
211 get_lcinterface(Rt_map *lmp, Lc_interface *funcs)
212 {
213 	int		threaded = 0, entry = 0, tag;
214 	Lm_list		*lml;
215 	Lc_desc		*lcp;
216 
217 	if ((lmp == NULL) || (funcs == NULL))
218 		return;
219 
220 	/*
221 	 * Once the process is active, ensure we grab a lock.
222 	 */
223 	if (rtld_flags & RT_FL_APPLIC)
224 		entry = enter(0);
225 
226 	lml = LIST(lmp);
227 	lcp = &lml->lm_lcs[0];
228 
229 	DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
230 
231 	for (tag = funcs->ci_tag; tag; tag = (++funcs)->ci_tag) {
232 		char	*gptr;
233 		char	*lptr = funcs->ci_un.ci_ptr;
234 
235 		DBG_CALL(Dbg_util_lcinterface(lmp, tag, lptr));
236 
237 		if (tag >= CI_MAX)
238 			continue;
239 
240 		/*
241 		 * Maintain all interfaces on a per-link-map basis.  Note, for
242 		 * most interfaces, only the first interface is used for any
243 		 * link-map list.  This prevents accidents with developers who
244 		 * manage to load two different versions of libc.
245 		 */
246 		if ((lcp[tag].lc_lmp) &&
247 		    (tag != CI_LCMESSAGES) && (tag != CI_VERSION)) {
248 			DBG_CALL(Dbg_unused_lcinterface(lmp,
249 			    lcp[tag].lc_lmp, tag));
250 			continue;
251 		}
252 
253 		lcp[tag].lc_un.lc_ptr = lptr;
254 		lcp[tag].lc_lmp = lmp;
255 
256 		gptr = glcs[tag].lc_un.lc_ptr;
257 
258 		/*
259 		 * Process any interfaces that must be maintained on a global
260 		 * basis.
261 		 */
262 		switch (tag) {
263 		case CI_ATEXIT:
264 			break;
265 
266 		case CI_LCMESSAGES:
267 			/*
268 			 * At startup, ld.so.1 can establish a locale from one
269 			 * of the locale family of environment variables (see
270 			 * ld_str_env() and readenv_user()).  During process
271 			 * execution the locale can also be changed by the user.
272 			 * This interface is called from libc should the locale
273 			 * be modified.  Presently, only one global locale is
274 			 * maintained for all link-map lists, and only objects
275 			 * on the primrary link-map may change this locale.
276 			 */
277 			if ((lml->lm_flags & LML_FLG_BASELM) &&
278 			    ((gptr == NULL) || (strcmp(gptr, lptr) != 0))) {
279 				/*
280 				 * If we've obtained a message locale (typically
281 				 * supplied via libc's setlocale()), then
282 				 * register the locale for use in dgettext() so
283 				 * as to reestablish the locale for ld.so.1's
284 				 * messages.
285 				 */
286 				if (gptr) {
287 					free((void *)gptr);
288 					rtld_flags |= RT_FL_NEWLOCALE;
289 				}
290 				glcs[tag].lc_un.lc_ptr = strdup(lptr);
291 
292 				/*
293 				 * Clear any cached messages.
294 				 */
295 				bzero(err_strs, sizeof (err_strs));
296 				nosym_str = NULL;
297 			}
298 			break;
299 
300 		case CI_BIND_GUARD:
301 		case CI_BIND_CLEAR:
302 		case CI_THR_SELF:
303 		case CI_CRITICAL:
304 			/*
305 			 * If the global vector is unset, or this is the primary
306 			 * link-map, set the global vector.
307 			 */
308 			if ((gptr == NULL) || (lml->lm_flags & LML_FLG_BASELM))
309 				glcs[tag].lc_un.lc_ptr = lptr;
310 
311 			/* FALLTHROUGH */
312 
313 		case CI_TLS_MODADD:
314 		case CI_TLS_MODREM:
315 		case CI_TLS_STATMOD:
316 		case CI_THRINIT:
317 			threaded++;
318 			break;
319 
320 		case CI_VERSION:
321 			if ((rtld_flags2 & RT_FL2_RTLDSEEN) == 0) {
322 				Aliste	idx;
323 				Lm_list	*lml2;
324 				int	version;
325 
326 				rtld_flags2 |= RT_FL2_RTLDSEEN;
327 
328 				version = funcs->ci_un.ci_val;
329 #if defined(CI_V_FIVE)
330 				if (version >= CI_V_FIVE) {
331 					thr_flg_nolock = THR_FLG_NOLOCK;
332 					thr_flg_reenter = THR_FLG_REENTER;
333 				}
334 #endif
335 				if (version < CI_V_FOUR)
336 					break;
337 
338 				rtld_flags2 |= RT_FL2_UNIFPROC;
339 
340 				/*
341 				 * We might have seen an auditor which is not
342 				 * dependent on libc.  Such an auditor's link
343 				 * map list has LML_FLG_HOLDLOCK set.  This
344 				 * lock needs to be dropped.  Refer to
345 				 * audit_setup() in audit.c.
346 				 */
347 				if ((rtld_flags2 & RT_FL2_HASAUDIT) == 0)
348 					break;
349 
350 				/*
351 				 * Yes, we did.  Take care of them.
352 				 */
353 				for (APLIST_TRAVERSE(dynlm_list, idx, lml2)) {
354 					Rt_map *map = (Rt_map *)lml2->lm_head;
355 
356 					if (FLAGS(map) & FLG_RT_AUDIT) {
357 						lml2->lm_flags &=
358 						    ~LML_FLG_HOLDLOCK;
359 					}
360 				}
361 			}
362 			break;
363 
364 		default:
365 			break;
366 		}
367 	}
368 
369 	if (threaded) {
370 		/*
371 		 * If a version of libc gives us only a subset of the TLS
372 		 * interfaces, it's confused and we discard the whole lot.
373 		 */
374 		if (((lcp[CI_TLS_MODADD].lc_un.lc_func != NULL) &&
375 		    (lcp[CI_TLS_MODREM].lc_un.lc_func != NULL) &&
376 		    (lcp[CI_TLS_STATMOD].lc_un.lc_func != NULL)) == 0) {
377 			lcp[CI_TLS_MODADD].lc_un.lc_func = NULL;
378 			lcp[CI_TLS_MODREM].lc_un.lc_func = NULL;
379 			lcp[CI_TLS_STATMOD].lc_un.lc_func = NULL;
380 		}
381 
382 		/*
383 		 * Indicate that we're now thread capable.
384 		 */
385 		if ((lml->lm_flags & LML_FLG_RTLDLM) == 0)
386 			rtld_flags |= RT_FL_THREADS;
387 	}
388 
389 	if (entry)
390 		leave(lml, 0);
391 }
392 
393 /*
394  * At this point we know we have a set of objects that have been fully analyzed
395  * and relocated.  Prior to the next major step of running .init sections (ie.
396  * running user code), retrieve any RTLDINFO interfaces.
397  */
398 int
399 rt_get_extern(Lm_list *lml, Rt_map *lmp)
400 {
401 	if (lml->lm_rti) {
402 		Aliste		idx;
403 		Rti_desc	*rti;
404 
405 		for (ALIST_TRAVERSE(lml->lm_rti, idx, rti))
406 			get_lcinterface(rti->rti_lmp, rti->rti_info);
407 
408 		free(lml->lm_rti);
409 		lml->lm_rti = 0;
410 	}
411 
412 	/*
413 	 * Perform some sanity checks.  If we have TLS requirements we better
414 	 * have the associated external interfaces.
415 	 */
416 	if (lml->lm_tls &&
417 	    (lml->lm_lcs[CI_TLS_STATMOD].lc_un.lc_func == NULL)) {
418 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_TLS_NOSUPPORT),
419 		    NAME(lmp));
420 		return (0);
421 	}
422 	return (1);
423 }
424 
425 /*
426  * Provide an interface for libc to communicate additional interface
427  * information.
428  */
429 void
430 _ld_libc(void *ptr)
431 {
432 	get_lcinterface(_caller(caller(), CL_EXECDEF), (Lc_interface *)ptr);
433 }
434 
435 static int	bindmask = 0;
436 
437 int
438 rt_bind_guard(int flags)
439 {
440 	int	(*fptr)(int);
441 	int	bindflag;
442 
443 	if ((fptr = glcs[CI_BIND_GUARD].lc_un.lc_func) != NULL) {
444 		return ((*fptr)(flags));
445 	} else {
446 		bindflag = (flags & THR_FLG_RTLD);
447 		if ((bindflag & bindmask) == 0) {
448 			bindmask |= bindflag;
449 			return (1);
450 		}
451 		return (0);
452 	}
453 }
454 
455 int
456 rt_bind_clear(int flags)
457 {
458 	int	(*fptr)(int);
459 	int	bindflag;
460 
461 	if ((fptr = glcs[CI_BIND_CLEAR].lc_un.lc_func) != NULL) {
462 		return ((*fptr)(flags));
463 	} else {
464 		bindflag = (flags & THR_FLG_RTLD);
465 		if (bindflag == 0)
466 			return (bindmask);
467 		else {
468 			bindmask &= ~bindflag;
469 			return (0);
470 		}
471 	}
472 }
473 
474 /*
475  * Make sure threads have been initialized.  This interface is called once for
476  * each link-map list.
477  */
478 void
479 rt_thr_init(Lm_list *lml)
480 {
481 	int	(*fptr)(void);
482 
483 	if ((fptr = lml->lm_lcs[CI_THRINIT].lc_un.lc_func) != NULL) {
484 		lml->lm_lcs[CI_THRINIT].lc_un.lc_func = NULL;
485 
486 		leave(lml, thr_flg_reenter);
487 		(void) (*fptr)();
488 		(void) enter(thr_flg_reenter);
489 
490 		/*
491 		 * If this is an alternative link-map list, and this is the
492 		 * first call to initialize threads, don't let the destination
493 		 * libc be deleted.  It is possible that an auditors complete
494 		 * initialization fails, but there is presently no main link-map
495 		 * list.  As this libc has established the thread pointer, don't
496 		 * delete this libc, otherwise the initialization of libc on the
497 		 * main link-map can be compromised during its threads
498 		 * initialization.
499 		 */
500 		if (((lml->lm_flags & LML_FLG_BASELM) == 0) &&
501 		    ((rtld_flags2 & RT_FL2_PLMSETUP) == 0))
502 			MODE(lml->lm_lcs[CI_THRINIT].lc_lmp) |= RTLD_NODELETE;
503 	}
504 }
505 
506 thread_t
507 rt_thr_self()
508 {
509 	thread_t	(*fptr)(void);
510 
511 	if ((fptr = (thread_t (*)())glcs[CI_THR_SELF].lc_un.lc_func) != NULL)
512 		return ((*fptr)());
513 
514 	return (1);
515 }
516 
517 int
518 rt_mutex_lock(Rt_lock *mp)
519 {
520 	return (_lwp_mutex_lock((lwp_mutex_t *)mp));
521 }
522 
523 int
524 rt_mutex_unlock(Rt_lock *mp)
525 {
526 	return (_lwp_mutex_unlock((lwp_mutex_t *)mp));
527 }
528 
529 /*
530  * Test whether we're in a libc critical region.  Certain function references,
531  * like the "mem*" family, might require binding.  Although these functions can
532  * safely bind to auxiliary filtees, they should not be captured by auditors.
533  */
534 int
535 rt_critical()
536 {
537 	int	(*fptr)(void);
538 
539 	if ((fptr = glcs[CI_CRITICAL].lc_un.lc_func) != NULL)
540 		return ((*fptr)());
541 
542 	return (0);
543 }
544 
545 /*
546  * Mutex interfaces to resolve references from any objects extracted from
547  * libc_pic.a.  Note, as ld.so.1 is essentially single threaded these can be
548  * noops.
549  */
550 #pragma weak lmutex_lock = mutex_lock
551 /* ARGSUSED */
552 int
553 mutex_lock(mutex_t *mp)
554 {
555 	return (0);
556 }
557 
558 #pragma weak lmutex_unlock = mutex_unlock
559 /* ARGSUSED */
560 int
561 mutex_unlock(mutex_t *mp)
562 {
563 	return (0);
564 }
565 
566 /* ARGSUSED */
567 int
568 mutex_init(mutex_t *mp, int type, void *arg)
569 {
570 	return (0);
571 }
572 
573 /* ARGSUSED */
574 int
575 mutex_destroy(mutex_t *mp)
576 {
577 	return (0);
578 }
579 
580 /*
581  * This is needed to satisfy sysconf() (case _SC_THREAD_STACK_MIN)
582  */
583 size_t
584 thr_min_stack()
585 {
586 	return (sizeof (uintptr_t) * 1024);
587 }
588 
589 /*
590  * Local str[n]casecmp() interfaces for the dynamic linker,
591  * to avoid problems when linking with libc_pic.a
592  */
593 int
594 strcasecmp(const char *s1, const char *s2)
595 {
596 	extern int ascii_strcasecmp(const char *, const char *);
597 
598 	return (ascii_strcasecmp(s1, s2));
599 }
600 
601 int
602 strncasecmp(const char *s1, const char *s2, size_t n)
603 {
604 	extern int ascii_strncasecmp(const char *, const char *, size_t);
605 
606 	return (ascii_strncasecmp(s1, s2, n));
607 }
608 
609 /*
610  * The following functions are cancellation points in libc.
611  * They are called from other functions in libc that we extract
612  * and use directly.  We don't do cancellation while we are in
613  * the dynamic linker, so we redefine these to call the primitive,
614  * non-cancellation interfaces.
615  */
616 int
617 close(int fildes)
618 {
619 	extern int __close(int);
620 
621 	return (__close(fildes));
622 }
623 
624 int
625 fcntl(int fildes, int cmd, ...)
626 {
627 	extern int __fcntl(int, int, ...);
628 	intptr_t arg, arg1 = 0;
629 	va_list ap;
630 
631 	va_start(ap, cmd);
632 	switch (cmd) {
633 	case F_DUP3FD:
634 		arg = va_arg(ap, int);
635 		arg1 = va_arg(ap, int);
636 		break;
637 	default:
638 		arg = va_arg(ap, intptr_t);
639 		break;
640 	}
641 	va_end(ap);
642 	return (__fcntl(fildes, cmd, arg, arg1));
643 }
644 
645 int
646 open(const char *path, int oflag, ...)
647 {
648 	extern int __open(const char *, int, mode_t);
649 	mode_t mode;
650 	va_list ap;
651 
652 	va_start(ap, oflag);
653 	mode = va_arg(ap, mode_t);
654 	va_end(ap);
655 	return (__open(path, oflag, mode));
656 }
657 
658 int
659 openat(int fd, const char *path, int oflag, ...)
660 {
661 	extern int __openat(int, const char *, int, mode_t);
662 	mode_t mode;
663 	va_list ap;
664 
665 	va_start(ap, oflag);
666 	mode = va_arg(ap, mode_t);
667 	va_end(ap);
668 	return (__openat(fd, path, oflag, mode));
669 }
670 
671 ssize_t
672 read(int fd, void *buf, size_t size)
673 {
674 	extern ssize_t __read(int, void *, size_t);
675 	return (__read(fd, buf, size));
676 }
677 
678 ssize_t
679 write(int fd, const void *buf, size_t size)
680 {
681 	extern ssize_t __write(int, const void *, size_t);
682 	return (__write(fd, buf, size));
683 }
684 
685 /*
686  * ASCII versions of ctype character classification functions.  This avoids
687  * pulling in the entire locale framework that is in libc.
688  */
689 
690 int
691 isdigit(int c)
692 {
693 	return ((c >= '0' && c <= '9') ? 1 : 0);
694 }
695 
696 int
697 isupper(int c)
698 {
699 	return ((c >= 'A' && c <= 'Z') ? 1 : 0);
700 }
701 
702 int
703 islower(int c)
704 {
705 	return ((c >= 'a' && c <= 'z') ? 1 : 0);
706 }
707 
708 int
709 isspace(int c)
710 {
711 	return (((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n') ||
712 	    (c == '\v') || (c == '\f')) ? 1 : 0);
713 }
714 
715 int
716 isxdigit(int c)
717 {
718 	return ((isdigit(c) || (c >= 'A' && c <= 'F') ||
719 	    (c >= 'a' && c <= 'f')) ? 1 : 0);
720 }
721 
722 int
723 isalpha(int c)
724 {
725 	return ((isupper(c) || islower(c)) ? 1 : 0);
726 }
727 
728 int
729 isalnum(int c)
730 {
731 	return ((isalpha(c) || isdigit(c)) ? 1 : 0);
732 }
733 
734 #if defined(__i386) || defined(__amd64)
735 /*
736  * Instead of utilizing the comm page for clock_gettime and gettimeofday, rtld
737  * uses the raw syscall instead.  Doing so decreases the surface of symbols
738  * needed from libc for a modest performance cost.
739  */
740 extern int __clock_gettime_sys(clockid_t, struct timespec *);
741 
742 int
743 __clock_gettime(clockid_t clock_id, struct timespec *tp)
744 {
745 	return (__clock_gettime_sys(clock_id, tp));
746 }
747 
748 int
749 gettimeofday(struct timeval *tv, void *tz)
750 {
751 	if (tv != NULL) {
752 		/*
753 		 * Perform the same logic as the libc gettimeofday() when it
754 		 * lacks comm page support: Make the clock_gettime syscall and
755 		 * divide out the tv_usec field as required.
756 		 */
757 		(void) __clock_gettime_sys(CLOCK_REALTIME, (timespec_t *)tv);
758 		tv->tv_usec /= 1000;
759 	}
760 
761 	return (0);
762 }
763 #endif /* defined(__i386) || defined(__amd64) */
764 
765 /*
766  * In a similar vein to the is* functions above, we also have to define our own
767  * version of strerror, as it is implemented in terms of the locale aware
768  * strerror_l, and we'd rather not have the full set of libc symbols used here.
769  */
770 char *
771 strerror(int errnum)
772 {
773 	if (errnum < _sys_num_nerr && errnum >= 0) {
774 		return (dgettext("SUNW_OST_OSLIB",
775 		    &_sys_nerrs[_sys_nindex[errnum]]));
776 	}
777 
778 	errno = EINVAL;
779 	return (dgettext("SUNW_OST_OSLIB", "Unknown error"));
780 }
781