/*-1* Copyright (c) 2019 Leandro Lupori2* Copyright (c) 2021 The FreeBSD Foundation3*4* Portions of this software were developed by Andrew Turner5* under sponsorship from the FreeBSD Foundation.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <sys/cdefs.h>27#include <machine/ifunc.h>2829static __ifunc_arg_t ifunc_arg;3031static void32ifunc_init(const Elf_Auxinfo *aux)33{34ifunc_arg._size = sizeof(ifunc_arg);35ifunc_arg._hwcap = 0;36ifunc_arg._hwcap2 = 0;37ifunc_arg._hwcap3 = 0;38ifunc_arg._hwcap4 = 0;3940for (; aux->a_type != AT_NULL; aux++) {41switch (aux->a_type) {42case AT_HWCAP:43ifunc_arg._hwcap = aux->a_un.a_val | _IFUNC_ARG_HWCAP;44break;45case AT_HWCAP2:46ifunc_arg._hwcap2 = aux->a_un.a_val;47break;48case AT_HWCAP3:49ifunc_arg._hwcap3 = aux->a_un.a_val;50break;51case AT_HWCAP4:52ifunc_arg._hwcap4 = aux->a_un.a_val;53break;54}55}56}5758static void59crt1_handle_rela(const Elf_Rela *r)60{61typedef Elf_Addr (*ifunc_resolver_t)(62uint64_t, const __ifunc_arg_t *, uint64_t, uint64_t,63uint64_t, uint64_t, uint64_t, uint64_t);64Elf_Addr *ptr, *where, target;6566switch (ELF_R_TYPE(r->r_info)) {67case R_AARCH64_IRELATIVE:68ptr = (Elf_Addr *)r->r_addend;69where = (Elf_Addr *)r->r_offset;70target = ((ifunc_resolver_t)ptr)(ifunc_arg._hwcap, &ifunc_arg, 0, 0, 0, 0, 0, 0);71*where = target;72break;73}74}757677