/*-1* Copyright (c) 2019 Leandro Lupori2* Copyright (c) 2024 Jessica Clarke <[email protected]>3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND11* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE12* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE13* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE14* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL15* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS16* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)17* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT18* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY19* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF20* SUCH DAMAGE.21*/2223static unsigned long elf_hwcap;2425static void26ifunc_init(const Elf_Auxinfo *aux)27{28/* Digest the auxiliary vector. */29for (; aux->a_type != AT_NULL; aux++) {30switch (aux->a_type) {31case AT_HWCAP:32elf_hwcap = (uint32_t)aux->a_un.a_val;33break;34}35}36}3738static void39crt1_handle_rela(const Elf_Rela *r)40{41typedef Elf_Addr (*ifunc_resolver_t)(42unsigned long, unsigned long, unsigned long, unsigned long,43unsigned long, unsigned long, unsigned long, unsigned long);44Elf_Addr *ptr, *where, target;4546switch (ELF_R_TYPE(r->r_info)) {47case R_RISCV_IRELATIVE:48ptr = (Elf_Addr *)r->r_addend;49where = (Elf_Addr *)r->r_offset;50target = ((ifunc_resolver_t)ptr)(elf_hwcap,510, 0, 0, 0, 0, 0, 0);52*where = target;53break;54}55}565758