xref: /freebsd/libexec/rtld-elf/rtld-libc/rtld_libc.c (revision 180df764c4ee570850deb4a910a11780ec3316df)
1b54a59f3SAlex Richardson /*-
2b54a59f3SAlex Richardson  * SPDX-License-Identifier: BSD-2-Clause
3b54a59f3SAlex Richardson  *
4e1470c8fSAlex Richardson  * Copyright 2019 Alex Richardson <arichardson@FreeBSD.org>
5b54a59f3SAlex Richardson  *
6b54a59f3SAlex Richardson  * This software was developed by SRI International and the University of
7b54a59f3SAlex Richardson  * Cambridge Computer Laboratory (Department of Computer Science and
8b54a59f3SAlex Richardson  * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the
9b54a59f3SAlex Richardson  * DARPA SSITH research programme.
10b54a59f3SAlex Richardson  *
11b54a59f3SAlex Richardson  * Redistribution and use in source and binary forms, with or without
12b54a59f3SAlex Richardson  * modification, are permitted provided that the following conditions
13b54a59f3SAlex Richardson  * are met:
14b54a59f3SAlex Richardson  * 1. Redistributions of source code must retain the above copyright
15b54a59f3SAlex Richardson  *    notice, this list of conditions and the following disclaimer.
16b54a59f3SAlex Richardson  * 2. Redistributions in binary form must reproduce the above copyright
17b54a59f3SAlex Richardson  *    notice, this list of conditions and the following disclaimer in the
18b54a59f3SAlex Richardson  *    documentation and/or other materials provided with the distribution.
19b54a59f3SAlex Richardson  *
20b54a59f3SAlex Richardson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21b54a59f3SAlex Richardson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22b54a59f3SAlex Richardson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23b54a59f3SAlex Richardson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24b54a59f3SAlex Richardson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25b54a59f3SAlex Richardson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26b54a59f3SAlex Richardson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27b54a59f3SAlex Richardson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28b54a59f3SAlex Richardson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29b54a59f3SAlex Richardson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30b54a59f3SAlex Richardson  * SUCH DAMAGE.
31b54a59f3SAlex Richardson  */
32a6fe717cSWarner Losh 
33b54a59f3SAlex Richardson #include <sys/param.h>
34b54a59f3SAlex Richardson #include <sys/types.h>
35b54a59f3SAlex Richardson #include <sys/sysctl.h>
36b54a59f3SAlex Richardson #include <assert.h>
37b54a59f3SAlex Richardson #include <signal.h>
38b54a59f3SAlex Richardson #include <stdarg.h>
39b54a59f3SAlex Richardson #include <stdlib.h>
40b54a59f3SAlex Richardson #include <string.h>
41b54a59f3SAlex Richardson #include <stdio.h>
42b54a59f3SAlex Richardson 
43b54a59f3SAlex Richardson #include "rtld.h"
44b54a59f3SAlex Richardson #include "rtld_printf.h"
45b54a59f3SAlex Richardson #include "rtld_libc.h"
46b54a59f3SAlex Richardson 
47b54a59f3SAlex Richardson /*
48b54a59f3SAlex Richardson  * Avoid dependencies from various libc calls on abort(). Since this is only
49b54a59f3SAlex Richardson  * used for assertions in RTLD, we can just raise SIGABRT directly.
50b54a59f3SAlex Richardson  */
51b54a59f3SAlex Richardson void
abort(void)52b54a59f3SAlex Richardson abort(void)
53b54a59f3SAlex Richardson {
54b54a59f3SAlex Richardson 	raise(SIGABRT);
55b54a59f3SAlex Richardson 	__builtin_trap();
56b54a59f3SAlex Richardson }
57b54a59f3SAlex Richardson 
58b54a59f3SAlex Richardson static int rtld_errno;
59b54a59f3SAlex Richardson int *__error(void);
60b54a59f3SAlex Richardson int *
__error(void)61b54a59f3SAlex Richardson __error(void)
62b54a59f3SAlex Richardson {
63b54a59f3SAlex Richardson 
64b54a59f3SAlex Richardson 	return (&rtld_errno);
65b54a59f3SAlex Richardson }
66b54a59f3SAlex Richardson 
67b54a59f3SAlex Richardson /* Avoid dependency on __libc_interposing, use the system call directly. */
68b54a59f3SAlex Richardson #undef sigprocmask
69b54a59f3SAlex Richardson int
sigprocmask(int how,const sigset_t * set,sigset_t * oset)70b54a59f3SAlex Richardson sigprocmask(int how, const sigset_t *set, sigset_t *oset)
71b54a59f3SAlex Richardson {
72b54a59f3SAlex Richardson 
73b54a59f3SAlex Richardson 	return (__sys_sigprocmask(how, set, oset));
74b54a59f3SAlex Richardson }
75b54a59f3SAlex Richardson __strong_reference(sigprocmask, __libc_sigprocmask);
76b54a59f3SAlex Richardson 
77b54a59f3SAlex Richardson #if defined DEBUG || !defined(NDEBUG)
78b54a59f3SAlex Richardson /* Provide an implementation of __assert that does not pull in fprintf() */
79b54a59f3SAlex Richardson void
__assert(const char * func,const char * file,int line,const char * failedexpr)80b54a59f3SAlex Richardson __assert(const char *func, const char *file, int line, const char *failedexpr)
81b54a59f3SAlex Richardson {
82b54a59f3SAlex Richardson 
83b54a59f3SAlex Richardson 	if (func == NULL)
84b54a59f3SAlex Richardson 		(void)rtld_fdprintf(STDERR_FILENO,
85b54a59f3SAlex Richardson 		    "Assertion failed: (%s), file %s, line %d.\n", failedexpr,
86b54a59f3SAlex Richardson 		    file, line);
87b54a59f3SAlex Richardson 	else
88b54a59f3SAlex Richardson 		(void)rtld_fdprintf(STDERR_FILENO,
89b54a59f3SAlex Richardson 		    "Assertion failed: (%s), function %s, file %s, line %d.\n",
90b54a59f3SAlex Richardson 		    failedexpr, func, file, line);
91b54a59f3SAlex Richardson 	abort();
92b54a59f3SAlex Richardson 	/* NOTREACHED */
93b54a59f3SAlex Richardson }
94b54a59f3SAlex Richardson #endif
95b54a59f3SAlex Richardson 
96b54a59f3SAlex Richardson /*
97b54a59f3SAlex Richardson  * Avoid pulling in all of pthreads from getpagesize().
98b54a59f3SAlex Richardson  * It normally uses libc/gen/auxv.c which pulls in pthread_once().
99e85eaa93SAndrew Turner  * This relies on init_pagesizes setting page_size so must not be called
100e85eaa93SAndrew Turner  * before that.
101b54a59f3SAlex Richardson  */
102b54a59f3SAlex Richardson int
getpagesize(void)103b54a59f3SAlex Richardson getpagesize(void)
104b54a59f3SAlex Richardson {
105e85eaa93SAndrew Turner 	return (page_size);
106b54a59f3SAlex Richardson }
107b54a59f3SAlex Richardson 
108*180df764SKonstantin Belousov int __sys___sysctl(const int *name, u_int namelen, void *oldp,
109b54a59f3SAlex Richardson     size_t *oldlenp, const void *newp, size_t newlen);
110b54a59f3SAlex Richardson 
111b54a59f3SAlex Richardson int
sysctl(const int * name,u_int namelen,void * oldp,size_t * oldlenp,const void * newp,size_t newlen)112b54a59f3SAlex Richardson sysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp,
113b54a59f3SAlex Richardson     const void *newp, size_t newlen)
114b54a59f3SAlex Richardson {
115b54a59f3SAlex Richardson 	int retval;
116b54a59f3SAlex Richardson 
117b54a59f3SAlex Richardson 	assert(name[0] != CTL_USER && "Not supported inside rtld!");
118b54a59f3SAlex Richardson 	retval = __sys___sysctl(name, namelen, oldp, oldlenp, newp, newlen);
119b54a59f3SAlex Richardson 	return (retval);
120b54a59f3SAlex Richardson }
121