Path: blob/main/libexec/rtld-elf/rtld-libc/rtld_libc.c
34923 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright 2019 Alex Richardson <[email protected]>4*5* This software was developed by SRI International and the University of6* Cambridge Computer Laboratory (Department of Computer Science and7* Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the8* DARPA SSITH research programme.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15* 2. Redistributions in binary form must reproduce the above copyright16* notice, this list of conditions and the following disclaimer in the17* documentation and/or other materials provided with the distribution.18*19* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND20* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE21* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE22* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE23* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL24* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS25* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT27* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY28* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF29* SUCH DAMAGE.30*/3132#include <sys/param.h>33#include <sys/types.h>34#include <sys/sysctl.h>35#include <assert.h>36#include <signal.h>37#include <stdarg.h>38#include <stdlib.h>39#include <string.h>40#include <stdio.h>4142#include "rtld.h"43#include "rtld_printf.h"44#include "rtld_libc.h"4546/*47* Avoid dependencies from various libc calls on abort(). Since this is only48* used for assertions in RTLD, we can just raise SIGABRT directly.49*/50void51abort(void)52{53raise(SIGABRT);54__builtin_trap();55}5657static int rtld_errno;58int *__error(void);59int *60__error(void)61{6263return (&rtld_errno);64}6566/* Avoid dependency on __libc_interposing, use the system call directly. */67#undef sigprocmask68int69sigprocmask(int how, const sigset_t *set, sigset_t *oset)70{7172return (__sys_sigprocmask(how, set, oset));73}74__strong_reference(sigprocmask, __libc_sigprocmask);7576#if defined DEBUG || !defined(NDEBUG)77/* Provide an implementation of __assert that does not pull in fprintf() */78void79__assert(const char *func, const char *file, int line, const char *failedexpr)80{8182if (func == NULL)83(void)rtld_fdprintf(STDERR_FILENO,84"Assertion failed: (%s), file %s, line %d.\n", failedexpr,85file, line);86else87(void)rtld_fdprintf(STDERR_FILENO,88"Assertion failed: (%s), function %s, file %s, line %d.\n",89failedexpr, func, file, line);90abort();91/* NOTREACHED */92}93#endif9495/*96* Avoid pulling in all of pthreads from getpagesize().97* It normally uses libc/gen/auxv.c which pulls in pthread_once().98* This relies on init_pagesizes setting page_size so must not be called99* before that.100*/101int102getpagesize(void)103{104return (page_size);105}106107int __sys___sysctl(const int *name, u_int namelen, void *oldp,108size_t *oldlenp, const void *newp, size_t newlen);109110int111sysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp,112const void *newp, size_t newlen)113{114int retval;115116assert(name[0] != CTL_USER && "Not supported inside rtld!");117retval = __sys___sysctl(name, namelen, oldp, oldlenp, newp, newlen);118return (retval);119}120121122