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